iOS--小知识点

iOS--小知识点

一. 颜色渐变

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor grayColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
gradientLayer.locations = @[@0.0, @0.5, @1.0];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1.0, 0);
gradientLayer.frame = CGRectMake(0, 100, 300, 2);
[self.view.layer addSublayer:gradientLayer];

三. 使用drowRect绘制简单图形

//获取上下文    CGContextRef 
context=UIGraphicsGetCurrentContext();    //设置绘制地区的颜色  
CGContextSetRGBFillColor(context, 1, 0, 0, 1);    //设置绘制的位置和大小    
CGContextFillRect(context, CGRectMake(0, 100, 100, 100)); 
NSString * text=@"文字";    
UIFont * font=[UIFont systemFontOfSize:14];
[text drawAtPoint:CGPointMake(0, 200) withAttributes:font.fontDescriptor.fontAttributes];    
UIImage *img=[UIImage imageNamed:@""];    
[img drawInRect:CGRectMake(0, 300, 100, 100)];

四. 可变数组与不可变数组之间的转换

NSMutableArray * mutablearray=[[NSMutableArray alloc]initWithCapacity:0];   
NSArray *copyarray=[mutablearray copy];    
copyarray=@[@"1",@"2",@"3"];    
NSArray *array = [[NSArray alloc]init];    
NSMutableArray *copymutablearray=[array mutableCopy];    
[copymutablearraysetObject:@"加入到第一位"atIndexedSubscript:0];    
[copymutablearray addObject:@"排序加入"];    
[copymutablearray addObjectsFromArray:copyarray];

五. 快速求和,最大值,最小值,平均值

NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];    
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"]floatValue];    
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"]floatValue];    
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"]floatValue];    
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"]floatValue];

六. 对控件的旋转,平移,缩放,复位

//平移
CGAffineTransform transForm = _pingyibu.transform;
_pingyibu.transform=CGAffineTransformTranslate(transForm, 100, 0);

//旋转
_pingyibu.transform = CGAffineTransformRotate(transForm, M_PI_4);

//缩放按钮
_pingyibu.transform = CGAffineTransformScale(transForm, 1.2, 1.2);

//初始化复位
_pingyibu.transform = CGAffineTransformIdentity;

七. 设置label的行间距

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_xuanzhuanla.text];    
NSMutableParagraphStyle *paragraphStyle =  [[NSMutableParagraphStyle alloc] init];  
[paragraphStylesetLineSpacing:3];    //调整行间距  
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,[_xuanzhuanla.text length])];    
_xuanzhuanla.attributedText = attributedString;

八. 让应用直接闪退

AppDelegate *app = [UIApplication sharedApplication].delegate;  
UIWindow *window = app.window;    
[UIView animateWithDuration:1.0f animations:^{
    window.alpha = 0;
} completion:^(BOOL finished) {
    exit(0);
}];

九. 手机震动

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

十一. label自适应高度

CGRect rect = [la.text boundingRectWithSize:CGSizeMake(WIDTH-70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:la.font} context:nil];

十三. image转换成data类型

NSData *data=UIImagePNGRepresentation(image);
NSData *data=UIImageJPEGRepresentation(image, 1.0);

十四. 获取当前时间和时间戳

//当前时间NSDate *senddate=[NSDate date];        
NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init]; 
[dateformattersetDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *locationString=[dateformatter stringFromDate:senddate];

//当前时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];        
NSTimeInterval a=[dat timeIntervalSince1970];        
NSString *timeString = [NSString stringWithFormat:@"%f", a];       
long s = [timeString intValue];

十五. 对比时间差

-(int)max:(NSDate *)datatime {    
//创建日期格式化对象    
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; 
[dateFormattersetDateFormat:@"YYYY-MM-dd hh:mm:ss"];  
NSDate *senddate=[NSDate date];    
NSString *locationString=[dateFormatter stringFromDate:senddate];    
NSDate *date=[dateFormatter dateFromString:locationString];    
//取两个日期对象的时间间隔:    
//这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;   
NSTimeInterval time=[date timeIntervalSinceDate:datatime];    
int days=((int)time)/(3600*24);
return days;
}

十六. 使提示框消失(定时)

[alu dismissWithClickedButtonIndex:0 animated:NO];

十七. 设置self.title的颜色

UIColor *color = [UIColor whiteColor];
NSDictionary * dict=[NSDictionary dictionaryWithObject:colorforKey:UITextAttributeTextColor];
self.navigationController.navigationBar.titleTextAttributes = dict;

十八. 请求定位权限以及定位属性

//定位管理器    
_locationManager = [[CLLocationManager alloc]init];    
//如果没有授权则请求用户授权
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){ 
[_locationManager requestWhenInUseAuthorization];    
} else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){       
 //设置代理        
_locationManager.delegate=self;        
//设置定位精度       
 _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
//定位频率,每隔多少米定位一次        
CLLocationDistance distance=10.0;
_locationManager.distanceFilter = distance;       
//启动跟踪定位      
[_locationManager startUpdatingLocation];   
 }

十九. 通过经纬度计算距离

BOOL JuLi = NO;    
CLLocation *orig = [[CLLocation alloc] initWithLatitude:oldlat  longitude:oldlong];    
CLLocation* dist = [[CLLocation alloc] initWithLatitude:nowlat longitude:nowlong];    
CLLocationDistance kilometers = [orig distanceFromLocation:dist];    
NSLog(@"距离:%f",kilometers);
if(kilometers >= 10) {
     JuLi=YES;    
}else{        
     JuLi=NO;    
}
return JuLi;

二十. 弹出效果

/** 弹出对话框的动画  动画时长
@param changeOutView
@param dur          
/*
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{    
CAKeyframeAnimation *animation;    
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];    
animation.duration = dur;    
animation.delegate = self;    
animation.removedOnCompletion = NO;    
animation.fillMode = kCAFillModeForwards;    
NSMutableArray *values = [NSMutableArray array];    
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];    
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];    
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];    
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];    
animation.values = values;    
animation.timingFunction = [CAMediaTimingFunctionfunctionWithName: @"easeInEaseOut"];   [changeOutView.layer addAnimation:animationforKey:nil];
}

二十一. 设置TextField左右视图

-(id)initWithFrame:(CGRect)frame drawingLeft:(UIImageView *)icon drawingRight:(UIButton *)bu{
self = [super initWithFrame:frame];
if(self) {        
   self.leftView = icon;        
   self.leftViewMode = UITextFieldViewModeAlways;           
   self.rightView= bu;        
   self.rightViewMode=UITextFieldViewModeAlways;    
}
   return self;
}
-(CGRect)leftViewRectForBounds:(CGRect)bounds{    
   CGRect iconRect = [super leftViewRectForBounds:bounds];        
   iconRect.origin.x += 5;
   return iconRect;
}
-(CGRect)rightViewRectForBounds:(CGRect)bounds{    
   CGRect burect = [super rightViewRectForBounds:bounds];    
   burect.origin.x +=-10;
   return burect;
}

二十二. 常用宏定义

#define PUSH(x) AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;[(UINavigationController*)appdelegate.window.rootViewController pushViewController:x animated:YES];
#define POP AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;[(UINavigationController*)appdelegate.window.rootViewController popViewControllerAnimated:YES];
#define POPROOT OGPAppDelegate *appdelegate=(OGPAppDelegate *)[UIApplication sharedApplication].delegate;[(UINavigationController*)appdelegate.window.rootViewController popToRootViewControllerAnimated:YES];
#define IOS7 ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0f)#define IPHONE_4INCH ([UIScreen mainScreen].bounds.size.height > 480.0f)
#define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]

二十三. 获取版本号提示版本更新

-(void)VersionButton{    
NSString * string=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://itunes.apple.com/lookup?id=??????????"] encoding:NSUTF8StringEncoding error:nil];
   if(string!=nil && [string length] > 0 && [string rangeOfString:@"version"].length==7) {        
   [self checkenAppUpdate:string];    
  }
}
-(void)checkenAppUpdate:(NSString *)appinfo{    
   NSString *version=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];    
   NSString *appInfo1=[appinfo substringFromIndex:[appinfo rangeOfString:@"\"version\":"].location+10];   
   appInfo1 = [[appInfo1 substringToIndex:[appInfo1 rangeOfString:@","].location] stringByReplacingOccurrencesOfString:@"\""withString:@""];
   if(![appInfo1 isEqualToString:version]) {        
      UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"" message [NSString stringWithFormat:@"新版本%@, 已经发布",appInfo1] delegate:self cancelButtonTitle:@"知道了"otherButtonTitles: nil];   
      alert.delegate=self;        
      [alert addButtonWithTitle:@"前往更新"];       
      [alert show];        
      alert.tag=20;    
   }
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
   if(buttonIndex==1 & alertView.tag==20) {        
       NSString *url=@"https://appsto.re/cn/-naScb.i";       
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];    
   }
}

二十四. 获取设备或APP信息

//返回应用程序名称
+(NSString *) getAppName{    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
   return[infoDictionary objectForKey:@"CFBundleDisplayName"];
}
//返回应用版本号+(NSString *) getAppVersion{    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
   return[infoDictionary objectForKey:@"CFBundleShortVersionString"];
}
+(NSString *) getAppBundleVersion{    
   NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
   return [infoDictionary objectForKey:@"CFBundleVersion"];
}
//设备型号
+(NSString *) getSystemName{
   return [[UIDevice currentDevice] systemName];
}
//设备版本号
+(NSString *) getSystemVersion{
   return[[UIDevice currentDevice] systemVersion];
}

二十五. 内存泄漏分析

静态分析内存泄露 使用Xcode自带的Analyze功能
(Product-> Analyze)(Shift + Command + B),对代码进行静态分析,
对于内存泄露(Potential Memory Leak), 未使用局部变量(dead store),
逻辑错误(Logic Flaws)以及API使用问题(API-usage)等明确的展示出来。 
静态分析的内存泄露情况比较简单,开发者都能很快的解决。这里不做赘述。

动态分析内存泄露 使用Xcode自带的Profile功能
(Product-> Profile)(Command + i)弹出工具框,
选择Leaks打开,选择运行设备点左上角的Record录制按钮,
项目就会在已选好的设备上运行,并开始录制内存检测情况。
选Leaks查看泄露情况,在Leaks的详细菜单Details选项里选调用树Call Tree,
可查看所有内存泄露发生在哪些地方。
再在右侧的齿轮设置-Call Tree-勾选Hide System Libraries,
则可直接看内存泄露发生的函数名、方法名。
点击函数名、方法名,可直接跳到函数方法的细节,
可以看到哪一句代码出现了内存泄露,以及泄露了多少内存。

接下来就要回到Xcode,找到出现内存泄露的函数方法,仔细分析如何出现的内存泄露;
 一般使用ARC,按照上面一提到的内存理解和编码习惯是不会出现内存泄露的。
但我们在开发过程中,经常要使用第三方的一些类库,特别是涉及到加密的类库,
用c或c++来编码的加密解密方法,会出现内存泄露。
此时,我们要明白这些内存分配,需要手动释放。
要一步一步看,哪里分配了内存,在使用完之后一定要记得释放free它。

二十六. block循环利用导致内存泄漏

A *a = [[A alloc] init]; 
a.finishBlock = ^(NSDictionary paraments){
    [adoSomething]; 
}; 
//对于这种在block块里引用对象a的情况,要在创建对象a时加修饰符block来防止循环引用。 
block A *a = [[A alloc] init]; 
a.finishBlock = ^(NSDictionary paraments){
   [a doSomething]; 
}; 
//或 
A __block a = [[A alloc] init]; 
a.finishBlock = ^(NSDictionary paraments){ 
   [a doSomething]; 
}; 
//或者,需要在block中调用用self的方法、属性、变量时,可以这样写
__weak typeof(self) weakSelf = self;
//在block中使用weakSelf。 
__weak typeof(self) weakSelf = self;
[bdoSomethingWithFinishBlock:^{ 
   weakSelf.text = @"内存检测"; 
   [weakSelfdoSomething]; 
}];

二十七. label 的空格属性和局部字段颜色

//设置 label 的 autoresizingMask 为 NO ,可以使用空格间隔字符串!  
NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:@"点击注册按钮服务协议"];      
//获取要调整颜色的文字位置,调整颜色      
NSRange range1=[[hintString string]rangeOfString:@"注册"];     
[hintString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range1];      
NSRange range2=[[hintString string]rangeOfString:@"服务协议"]; 
[hintString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range2];     
hintLabel.attributedText=hintString;

二十八. 过滤特殊字符

// 定义一个特殊字符的集合    
NSCharacterSet *set= [NSCharacterSet characterSetWithCharactersInString:    @"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];    
// 过滤字符串的特殊字符    
NSString *newString = [trimString stringByTrimmingCharactersInSet:set];

二十九. 设置滑动的时候隐藏 navigationbar

//第1种: 
navigationController.hidesBarsOnSwipe = Yes    
//第2种: 当我们的手离开屏幕时候隐藏    
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 
 if(velocity.y > 0){    
   [self.navigationControllersetNavigationBarHidden:YES animated:YES];        
} else {        
   [self.navigationControllersetNavigationBarHidden:NO animated:YES];        
}    
}    
//velocity.y这个量,在上滑和下滑时,变化极小(小数),但是因为方向不同,有正负之分,这就很好处理了。

三十. 屏幕截图

// 1. 开启一个与图片相关的图形上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0.0);
// 2. 获取当前图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();=
// 3. 获取需要截取的view的layer
[self.view.layer renderInContext:ctx];
// 4. 从当前上下文中获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 5. 关闭图形上下文
UIGraphicsEndImageContext();
// 6. 把图片保存到相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

三十一. 去掉 tabbar 和 navgationbar 的黑线

//去掉tabBar顶部线条    
CGRect rect = CGRectMake(0, 0, 1, 1);    UIGraphicsBeginImageContext(rect.size);    
CGContextRef context = UIGraphicsGetCurrentContext();  
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);    
CGContextFillRect(context, rect);    
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();  
[self.tabBarsetBackgroundImage:img];  
[self.tabBarsetShadowImage:img];  
//[self.navigationController.navigationBarsetBackgroundImage:img];    
//self.navigationController.navigationBar.shadowImage = ima;  
self.tabBar.backgroundColor = SLIVERYCOLOR;

三十二. 判断字符串中是否含有中文

-(BOOL)isChinese:(NSString *)str{        
   NSString *match=@"(^[\u4e00-\u9fa5]+$)";        
   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
   return [predicate evaluateWithObject:str];   
}

三十三. 解决父视图和子视图的手势冲突问题

/** 解决手势冲突 */    
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
   if([touch.view isDescendantOfView:cicView]) {
      returnNO;     
   } 
     returnYES;   
}

三十四. 获取当前连接的Wi-Fi的名称

NSString *wifiName = @"Not Found";        
CFArrayRef myArray = CNCopySupportedInterfaces();
   if(myArray != nil) {
      CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
   if(myDict != nil) {
      NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict);                
      wifiName = [dict valueForKey:@"SSID"];            
   }        
}

三十五. 跨控制器跳转返回

for(UIViewController *controllerinself.navigationController.viewControllers) {
   if([controller isKindOfClass:[@“你要返回的控制器类名” class]])
   {
      [self.navigationController popToViewController:controller animated:YES];        
   }    
}

三十六. layer.cornerRadius 圆角流畅性的优化

loginBtn.layer.rasterizationScale = [UIScreen mainScreen].scale;

三十七. 导航栏和下方视图间隔距离消失

self.automaticallyAdjustsScrollViewInsets=YES;

三十八. 毛玻璃效果(ios8.0以后的版本)

UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
visualEffectView.frame = CGRectMake(0, 0, 320*[FlexibleFrame ratio], 180*[FlexibleFrame ratio]);
visualEffectView.alpha = 1.0;

三十九. 移动工程不需要配置路径

创建.pch文件时 , BuildSetting 中的 Prefix Header 中的路径开头可以写 $(SRCROOT)/工程地址, 
这样写的好处可以使你的挪动代码不用手动更换路径!

四十. 判断两个数组是否相同

NSArray *array1 = [NSArray arrayWithObjects:@"a", @"b", @"c",  nil];    
NSArray *array2 = [NSArray arrayWithObjects:@"d", @"a", @"c",  nil];    
bool bol =false;    
//创建俩新的数组    
NSMutableArray *oldArr = [NSMutableArray arrayWithArray:array1];    
NSMutableArray *newArr = [NSMutableArray arrayWithArray:array2];    
//对数组1排序
[oldArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
   return obj1 > obj2;    
 }];    
//对数组2排序
[newArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
return obj1 > obj2;    
}];
if(newArr.count == oldArr.count) {        
   bol =true;
   for(int16_t i = 0; i < oldArr.count; i++) {            
      id c1 = [oldArr objectAtIndex:i];            
      id newc = [newArr objectAtIndex:i];
      if(![newc isEqualToString:c1]) {                
            bol =false;break;            
      }        
   }    
}
if(bol) {        
    NSLog(@" ------------- 两个数组的内容相同!");    
}else{        
    NSLog(@"-=-------------两个数组的内容不相同!");    
}

四十一. block

对于block,都应该使用copy来声明,
原因是block来捕获上下文的信息
[官方文档]
https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fios%2Fdocumentation%2FCocoa%2FConceptual%2FProgrammingWithObjectiveC%2FWorkingwithBlocks%2FWorkingwithBlocks.html%23%2F%2Fapple_ref%2Fdoc%2Fuid%2FTP40011210-CH8-SW12

四十三. 计算一段NSString在视图中渲染出来的尺寸

+ (CGSize)sizeOfString:(NSString *)textString font:(UIFont *)font bound:(CGSize)bound {      
   NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];     
   paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;      
   paragraphStyle.alignment = NSTextAlignmentLeft;
   if(font == nil) {          
      font = [UIFont systemFontOfSize:[UIFont systemFontSize]];         }      
   NSDictionary * attributes = @{NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle}; 
   NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:textString attributes:attributes];
   return [TTTAttributedLabel sizeThatFitsAttributedString:attributedString                                              withConstraints:CGSizeMake(bound.width, 999)                                      limitedToNumberOfLines:0];  
}

四十四. 获取相应的字体类型名

for(NSString *fontfamilynamein[UIFont familyNames])  { 
   NSLog(@"Family:'%@'",fontfamilyname);
   for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
      NSLog(@"\tfont:'%@'",fontName);      
   }      
   NSLog(@"~~~~~~~~");  
}

四十五. Masonry和SVProgreaaHUD需要注意

使用Masonry约束TTTAttributedLabel的时候,
初始化TTTAttributedLabel需要设置frame为CGRectZero,
否则约束会不生效。

SVProgressHUD如果要自定义一些属性,
比如loading状态转圈的颜色,
必须先设置style为SVProgressHUDStyleCustom,
这样自定义的状态才会生效。

四十六. 打印View所有子视图

po [[self view]recursiveDescription]

四十七. layoutSubviews调用的调用时机

* 当视图第一次显示的时候会被调用
* 当这个视图显示到屏幕上了,点击按钮
* 添加子视图也会调用这个方法
* 当本视图的大小发生改变的时候是会调用的
* 当子视图的frame发生改变的时候是会调用的
* 当删除子视图的时候是会调用的

四十八. UIImageView填充模式

@"UIViewContentModeScaleToFill",      // 拉伸自适应填满整个视图  
@"UIViewContentModeScaleAspectFit",  // 自适应比例大小显示  
@"UIViewContentModeScaleAspectFill",  // 原始大小显示  
@"UIViewContentModeRedraw",          // 尺寸改变时重绘  
@"UIViewContentModeCenter",          // 中间  
@"UIViewContentModeTop",              // 顶部  
@"UIViewContentModeBottom",          // 底部  
@"UIViewContentModeLeft",            // 中间贴左  
@"UIViewContentModeRight",            // 中间贴右  
@"UIViewContentModeTopLeft",          // 贴左上  
@"UIViewContentModeTopRight",        // 贴右上  
@"UIViewContentModeBottomLeft",      // 贴左下  
@"UIViewContentModeBottomRight",      // 贴右下

四十九. MRC和ARC混编设置方式

在XCode中targets的build phases选项下Compile Sources下选择 不需要arc编译的文件 
双击输入 -fno-objc-arc 即可 
MRC工程中也可以使用ARC的类,
方法如下: 
在XCode中targets的build phases选项下Compile Sources下选择要使用arc编译的文件 
双击输入 -fobjc-arc 即可

五十. 解决tableview的分割线短一截

-(void)viewDidLayoutSubviews { 
   if([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
      [self.tableViewsetSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
} if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
      [self.tableViewsetLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; 
   }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath { 
   if([cell respondsToSelector:@selector(setSeparatorInset:)]) { 
      [cellsetSeparatorInset:UIEdgeInsetsZero]; 
   } if ([cell respondsToSelector:@selector(setLayoutMargins:)]){
      [cellsetLayoutMargins:UIEdgeInsetsZero]; 
   }
}

五十一. 修改textFieldplaceholder字体颜色和大小

textField.placeholder = @"username is in here!";  
[textFieldsetValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];  
[textFieldsetValue:[UIFont boldSystemFontOfSize:16]forKeyPath:@"_placeholderLabel.font"];

五十二. 修改状态栏字体颜色

//只能设置两种颜色,黑色和白色,系统默认黑色
//设置为白色方法:
(1)在plist里面添加Status bar style,值为UIStatusBarStyleLightContent(白色)或UIStatusBarStyleDefault(黑 色)
(2)在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO

五十三.调用相机后状态栏消失

[[UIApplication sharedApplication]setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];

五十四. 修改UIAlertController字体颜色

//修改title  
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];  
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];  
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
if(alertController valueForKey:@"attributedTitle") {        
       [alertControllersetValue:alertControllerStrforKey:@"attributedTitle"];  
}  
//修改message  
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];  
[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];  
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
if(alertController valueForKey:@"attributedMessage") {
    [alertControllersetValue:alertControllerMessageStrforKey:@"attributedMessage"];  
}
//修改按钮字体颜色
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default"style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:defaultAction];
if(cancelAction valueForKey:@"titleTextColor") {
      [cancelActionsetValue:[UIColor redColor]forKey:@"titleTextColor"];  
}

五十五. 使用钥匙串保存设备唯一ID

//下载并倒入SSKeyChain的.h和.m文件,并添加Security.framework框架
+ (NSString *)getDeviceId{    
NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@" "account:@"uuid"];
   if(currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""]) {
      NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;        
      currentDeviceUUIDStr = currentDeviceUUID.UUIDString;        
      currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-"withString:@""];        
      currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];        
      [SSKeychainsetPassword: currentDeviceUUIDStrforService:@" "account:@"uuid"];    
   }
   return currentDeviceUUIDStr;
}

五十六. 获取设备型号

//引入头文件
#include #include

+ (NSString *)getCurrentDeviceModel{    
   int mib[2];    
   size_t len;    
   char *machine;        
   mib[0] = CTL_HW;    
   mib[1] = HW_MACHINE;    
   sysctl(mib, 2, NULL, &len, NULL, 0);    
   machine = malloc(len);    
   sysctl(mib, 2, machine, &len, NULL, 0);        
   NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];    
   free(machine);    
//iPhone 
if([platform isEqualToString:@"iPhone1,1"])return@"iPhone2G";
if([platform isEqualToString:@"iPhone1,2"])return@"iPhone3G";
if([platform isEqualToString:@"iPhone2,1"])return@"iPhone3GS";
if([platform isEqualToString:@"iPhone3,1"])return@"iPhone4";
if([platform isEqualToString:@"iPhone3,2"])return@"iPhone4";
if([platform isEqualToString:@"iPhone3,3"])return@"iPhone4";
if([platform isEqualToString:@"iPhone4,1"])return@"iPhone4S";
if([platform isEqualToString:@"iPhone5,1"])return@"iPhone5";
if([platform isEqualToString:@"iPhone5,2"])return@"iPhone5";
if([platform isEqualToString:@"iPhone5,3"])return@"iPhone5c";
if([platform isEqualToString:@"iPhone5,4"])return@"iPhone5c";
if([platform isEqualToString:@"iPhone6,1"])return@"iPhone5s";
if([platform isEqualToString:@"iPhone6,2"])return@"iPhone5s";
if([platform isEqualToString:@"iPhone7,2"])return@"iPhone6";
if([platform isEqualToString:@"iPhone7,1"])return@"iPhone6Plus";
if([platform isEqualToString:@"iPhone8,1"])return@"iPhone6s";
if([platform isEqualToString:@"iPhone8,2"])return@"iPhone6sPlus";
if([platform isEqualToString:@"iPhone8,3"])return@"iPhoneSE";
if([platform isEqualToString:@"iPhone8,4"])return@"iPhoneSE";
if([platform isEqualToString:@"iPhone9,1"])return@"iPhone7";
if([platform isEqualToString:@"iPhone9,2"])return@"iPhone7Plus";

 //iPod Touch
if([platform isEqualToString:@"iPod1,1"])return@"iPodTouch";
if([platform isEqualToString:@"iPod2,1"])return@"iPodTouch2G";
if([platform isEqualToString:@"iPod3,1"])return@"iPodTouch3G";
if([platform isEqualToString:@"iPod4,1"])return@"iPodTouch4G";
if([platform isEqualToString:@"iPod5,1"])return@"iPodTouch5G";
if([platform isEqualToString:@"iPod7,1"])return@"iPodTouch6G";    

//iPad 
if([platform isEqualToString:@"iPad1,1"])return@"iPad";
if([platform isEqualToString:@"iPad2,1"])return@"iPad2";
if([platform isEqualToString:@"iPad2,2"])return@"iPad2";
if([platform isEqualToString:@"iPad2,3"])return@"iPad2";
if([platform isEqualToString:@"iPad2,4"])return@"iPad2";
if([platform isEqualToString:@"iPad3,1"])return@"iPad3";
if([platform isEqualToString:@"iPad3,2"])return@"iPad3";
if([platform isEqualToString:@"iPad3,3"])return@"iPad3";
if([platform isEqualToString:@"iPad3,4"])return@"iPad4";
if([platform isEqualToString:@"iPad3,5"])return@"iPad4";
if([platform isEqualToString:@"iPad3,6"])return@"iPad4";  

//iPad Air 
if([platform isEqualToString:@"iPad4,1"])return@"iPadAir";
if([platform isEqualToString:@"iPad4,2"])return@"iPadAir";
if([platform isEqualToString:@"iPad4,3"])return@"iPadAir";
if([platform isEqualToString:@"iPad5,3"])return@"iPadAir2";
if([platform isEqualToString:@"iPad5,4"])return@"iPadAir2";        

//iPad mini
if([platform isEqualToString:@"iPad2,5"])return@"iPadmini1G";
if([platform isEqualToString:@"iPad2,6"])return@"iPadmini1G";
if([platform isEqualToString:@"iPad2,7"])return@"iPadmini1G";
if([platform isEqualToString:@"iPad4,4"])return@"iPadmini2";
if([platform isEqualToString:@"iPad4,5"])return@"iPadmini2";
if([platform isEqualToString:@"iPad4,6"])return@"iPadmini2";
if([platform isEqualToString:@"iPad4,7"])return@"iPadmini3";
if([platform isEqualToString:@"iPad4,8"])return@"iPadmini3";
if([platform isEqualToString:@"iPad4,9"])return@"iPadmini3";
if([platform isEqualToString:@"iPad5,1"])return@"iPadmini4";
if([platform isEqualToString:@"iPad5,2"])return@"iPadmini4";
if([platform isEqualToString:@"i386"])return@"iPhoneSimulator";
if([platform isEqualToString:@"x86_64"])return@"iPhoneSimulator";
return platform;
}

五十七. 减少发布之后过多的NSLog带来的性能影响

#if defined(DEBUG)||defined(_DEBUG)NSLog(@"测试代码");  
NSLog(@"Test Coding");
#endif

五十八. 去掉字符串的前后空格(基本用在textfield)

NSString *textName = [self.nameCell.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

五十九. 截取字符串到指定字符的位置

NSString *string = @"abcdefghijklmn";
NSRange range = [string rangeOfString:@"h"];
string = [string substringToIndex:range.location];

六十. 获取label的行数和每行的内容

- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label {    
   NSString *text = [label text];    
   UIFont *font = [label font];    
   CGRect rect = [label frame];    
   CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);    
   NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];    
   [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];    
   CFRelease(myFont);    
   CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);    
   CGMutablePathRef path = CGPathCreateMutable();    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));    
   CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);    
   NSArray *lines = ( NSArray *)CTFrameGetLines(frame); 
   NSMutableArray *linesArray = [[NSMutableArray alloc]init];
   for(id lineinlines) {        
      CTLineRef lineRef = (__bridge  CTLineRef )line;        
      CFRange lineRange = CTLineGetStringRange(lineRef);    
      NSRange range = NSMakeRange(lineRange.location, lineRange.length);        
      NSString *lineString = [text substringWithRange:range];           CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));    
      CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));        
//   NSLog(@"''''''''''''''''''%@",lineString);        
      [linesArray addObject:lineString];    
   }    
   CGPathRelease(path);    
   CFRelease( frame );    
   CFRelease(frameSetter);
   return(NSArray *)linesArray;
}

六十一.键盘透明

[UITextField alloc].keyboardAppearance = UIKeyboardAppearanceAlert;

六十二.状态栏的网络活动风火轮是否旋转,默认为NO

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

六十三.开启右滑返回

self.navigationController.interactivePopGestureRecognizer.delegete = nil;

六十四.MD5加密

#import "NSString+MD5.h"#import

- (NSString *)stringToMD5:(NSString *)str {
//1.首先将字符串转换成UTF-8编码, 因为MD5加密是基于C语言的,所以要先把字符串转化成C语言的字符串    
   const char *fooData = [str UTF8String];
//2.然后创建一个字符串数组,接收MD5的值    
   unsigned char result[CC_MD5_DIGEST_LENGTH];
//3.计算MD5的值, 这是官方封装好的加密方法:把我们输入的字符串转换成16进制的32位数,然后存储到result中    
   CC_MD5(fooData, (CC_LONG)strlen(fooData), result);    
/**    第一个参数:要加密的字符串    第二个参数: 获取要加密字符串的长度    第三个参数: 接收结果的数组    */
//4.创建一个字符串保存加密结果    
   NSMutableString *saveResult = [NSMutableString string];
//5.从result 数组中获取加密结果并放到 saveResult中 
   for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {        
      [saveResult appendFormat:@"%02x", result];    
   }    
/** x表示十六进制,%02X  意思是不足两位将用0补齐,如果多余两位则不影响        
NSLog("%02X", 0x888);  
//888        
NSLog("%02X", 0x4); 
//04    
*/
return saveResult;}

六十五.导航栏返回按钮颜色设置 及 文字变成<

-(void)UIBarButtonBackItemSet{    
[[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;   
[self.navigationController.navigationBarsetTintColor:[UIColor whiteColor]];}

六十六.字符串中包含双引号和反斜杠处理

NSString *testStr = @"你好,\"你好\"";  
NSLog(@"%@",testStr);  
//输出结果为:你好,"你好"
NSString *testStr = @"不好,\\不好\\\\"; 
NSLog(@"%@",testStr);
//输出结果为:不好,\不好\\
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容