####一. 颜色渐变
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]; [copymutablearray setObject:@"加入到第一位" 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]; [paragraphStyle setLineSpacing: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);复制代码
十. 生成手机唯一ID:UUID
-(NSString*) uuid { CFUUIDRef puuid = CFUUIDCreate( nil ); CFStringRef uuidString = CFUUIDCreateString( nil, puuid ); NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString)); CFRelease(puuid); CFRelease(uuidString); return result;}复制代码
十一. label自适应高度
CGRect rect=[la.text boundingRectWithSize:CGSizeMake(WIDTH-70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:la.font} context:nil];复制代码
十二. 使用AFN判断网络状态
NSURL *url=[NSURL URLWithString:@"http://www.apple.com"]; AFHTTPRequestOperationManager *operationManager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:url]; //根据不同的网络状态改变去做相应处理 [operationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusReachableViaWWAN: [self fasongsuoyou]; break; case AFNetworkReachabilityStatusReachableViaWiFi: [self fasongsuoyou]; break; case AFNetworkReachabilityStatusNotReachable: [self alert:NSLocalizedString(@"wuwangluo",@"")]; break; default: break; } }]; //开始监控 [operationManager.reachabilityManager startMonitoring];复制代码
十三. image转换成data类型
NSData *data=UIImagePNGRepresentation(image);NSData *data=UIImageJPEGRepresentation(image, 1.0);复制代码
十四. 获取当前时间和时间戳
//当前时间NSDate * senddate=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"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]; [dateFormatter setDateFormat:@"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:color forKey: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 要执行的view * @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 = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"]; [changeOutView.layer addAnimation:animation forKey: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;// 右偏10 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){ [a doSomething]; }; 对于这种在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; [b doSomethingWithFinishBlock:^{ weakSelf.text = @"内存检测"; [weakSelf doSomething]; }];复制代码
二十七. 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种: //1.当我们的手离开屏幕时候隐藏 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { if(velocity.y > 0) { [self.navigationController setNavigationBarHidden:YES animated:YES]; } else { [self.navigationController setNavigationBarHidden: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.tabBar setBackgroundImage:img]; [self.tabBar setShadowImage:img]; //[self.navigationController.navigationBar setBackgroundImage: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]) { return NO; } return YES; }复制代码
三十四. 获取当前连接的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 *controller in self.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来捕获上下文的信息
四十二. 使应用在后台运行
- (void)applicationDidEnterBackground:(UIApplication *)application { __block UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid; bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { bgTask = UIBackgroundTaskInvalid; } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { bgTask = UIBackgroundTaskInvalid; } }); }); } 复制代码
但其实不是应用一直在运行。
四十三. 计算一段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 *fontfamilyname in [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.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];}if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)]; }}-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {[cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {[cell setLayoutMargins:UIEdgeInsetsZero]; }}复制代码
五十一. 修改textFieldplaceholder字体颜色和大小
textField.placeholder = @"username is in here!"; [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [textField setValue:[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") { [alertController setValue:alertControllerStr forKey:@"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") { [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"]; }//修改按钮字体颜色UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:defaultAction]; if (cancelAction valueForKey:@"titleTextColor") { [cancelAction setValue:[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]; [SSKeychain setPassword: currentDeviceUUIDStr forService:@" "account:@"uuid"]; } return currentDeviceUUIDStr;}复制代码
五十六. 获取设备型号
引入头文件
#include <sys/types.h> #include <sys/sysctl.h>
+ (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 line in lines) { 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.delegate = 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.navigationBar setTintColor:[UIColor whiteColor]];}复制代码
六十六.字符串中包含双引号和反斜杠处理
NSString * testStr = @"你好,\"你好\""; NSLog(@"%@",testStr); //输出结果为:你好,"你好" NSString * testStr = @"不好,\\不好\\\\"; NSLog(@"%@",testStr);//输出结果为:不好,\不好\\复制代码
##待续!!复制代码