iOS小Tips

仅为我自己记录用的,一些乱七八糟的东西,有需要欢迎使用,觉得小白的大神请路过。。。

宏定义判断null字符
#define NSStringIsBlank(string) (![string isKindOfClass:[NSString class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""])
#define NSStringIsPresent(string) !NSStringIsBlank(string)
cocoapods
platform :ios, '8.0'
target '项目名称' do
pod 'AFNetworking' , '~> 3.0'
end
字符串-设置颜色不同
-(NSMutableAttributedString *)changeStrColor:(NSString *)string WithColor:(UIColor *)color WithLocation:(NSUInteger)location andLength:(NSUInteger)length
{    //将string格式变为attributedString    
      NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];    
     //设置color颜色 loc起始位置 len长度    
      [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)];    
      return attributedStr;
}
字符串 - 设置字体大小不同
-(NSMutableAttributedString *)changeStrFont:(NSString *)string withLength:(NSString *)length{    
      NSMutableAttributedString *attributedStr =[[NSMutableAttributedString alloc] initWithString:string];    
      [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(14, length.length)];      
       return attributedStr;
}
cell.painFront.attributedText = strFront;
数组倒序
NSArray *array = (NSMutableArray *)[[array reverseObjectEnumerator] allObjects];
字符串 - 根据字体大小改变label高度
-(CGFloat)getHeightWithText:(NSString *)text{    
      CGRect r = [text boundingRectWithSize:CGSizeMake(kScreenSize.width - 45 ,10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil];       
       return r.size.height;
}
提示框
#define IS_iOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0f
-(void)alertShowWithMsg:(NSString *)msg{  
        if (IS_iOS8) {       
           UIAlertController *tipVc  = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];     
           UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {            
        [self dismissViewControllerAnimated:YES completion:nil];        
}];       
       [tipVc addAction:cancleAction];       
       [self presentViewController:tipVc animated:YES completion:nil];             
   }else{  
       UIAlertView *tip = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];     
       [tip show];    
      }
}
[[UIView appearance] setTintColor:XK_COL_RGB(0x2edead)];//alertView的提示字体颜色
外框
[view.layer setBorderWidth:2.0];
[view.layer setBorderColor:[UIColor clearColor].CGColor];
切圆角
[view.layer setCornerRadius:Radius];
view.layer.masksToBounds = YES;
字符串 - 设置按钮的下划线
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"常见问题"];   
NSRange strRange = {0,[str length]};    
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_ProblemButton setAttributedTitle:str forState:UIControlStateNormal];
设置textview的placeholder
-(void)textViewDidChange:(UITextView *)textView
{
     if (textView.text.length == 0) {
          self.GreatLab.text = @"placeholder"; //自己设置的Label,用于placeholder
     } else {
          self.GreatLab.text = @"";
     }
}
找底层viewcontroller 父视图
-(UIViewController *)superViewController{    
      for (UIView *next = [self superview]; next; next = next.superview){        
            UIResponder *nextResponder = [next nextResponder];        
            if ([nextResponder isKindOfClass:[UIViewController class]]) {            
                  return (UIViewController *)nextResponder;        
            }   
        }    
    return nil;
}
找底层rootViewController 父视图
UIViewController *vc = [UIApplication sharedApplication].windows[0].rootViewController
拨打电话
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@“,@“电话号码"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
隐藏电话或银行卡中间几位
NSString *tel      = [self.attendant.payCard stringByReplacingCharactersInRange:NSMakeRange(4, 12) withString:@"************"];
cell.bankNub.text  = tel;
字符串 - 分割字符串
NSString *month = [NSString stringWithFormat:@"%@",self.walletListInfo.month];
NSMutableArray *strArray = [NSMutableArray array];
NSArray *array = [month componentsSeparatedByString:@"-"];
for (int i = 0; i < [array count]; i++) {
     NSLog(@"string:%@", [array objectAtIndex:i]);
     NSString *str = [array objectAtIndex:i];
    [strArray addObject:str];
}
根据日期查周几
NSDateComponents *_comps = [[NSDateComponents alloc] init];
[_comps setDay:[day integerValue]];
[_comps setMonth:[mouth integerValue]];
[_comps setYear:[year integerValue]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *_date = [gregorian dateFromComponents:_comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:_date];
int _weekday = (int)[weekdayComponents weekday];
判断用户是否允许通知
+(BOOL)isAllowedNotification {
    //iOS8 check if user allow notification
    float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (sysVer >= 8) {// system is iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone != setting.types) {
            return YES;
        }   
} else {//iOS7
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
       if(UIRemoteNotificationTypeNone != type)
           return YES;
   }   
return NO;
}
在工程中配置PCH文件,添加 $(TARGET_NAME)/$(TARGET_NAME).pch
iOS小Tips_第1张图片
添加PCH
刷新tableview的某个区
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
    [set addIndex:3];
    [set addIndex:4];
    [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
动画 - 旋转

点击旋转到上面,再点击再旋转到下方

-(void)transformAnimationWithfrom:(float)from To:(float)To image:(UIImageView *)image {    
  CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];    //默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果    
  animation.fromValue = [NSNumber numberWithFloat:from];    
  animation.toValue =  [NSNumber numberWithFloat:To];   
  animation.duration  = 0.5;    
  animation.autoreverses = NO;    
  animation.fillMode =kCAFillModeForwards;    
  animation.repeatCount = 1;    
  [image.layer addAnimation:animation forKey:nil];}

你可能感兴趣的:(iOS小Tips)