分享一些iOS开发实用的小技巧
作者:helloDolin
这篇文章主要给大家分享了一些iOS开发实用的小技巧,这些小技巧在大家开发iOS的时候还是相当实用,有需要的朋友们下面来一起看看吧。
1.设置navigationbar title颜色
UIColor *whiteColor = [UIColor whiteColor]; NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName]; [self.navigationController.navigationBar setTitleTextAttributes:dic];
2.获取UIColor RGB
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0]; const CGFloat *components = CGColorGetComponents(color.CGColor); NSLog(@"Red: %f", components[0]); NSLog(@"Green: %f", components[1]); NSLog(@"Blue: %f", components[2]); NSLog(@"Alpha: %f", components[3]);
3.修改textField的placeholder的字体颜色、大小
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
4.将color转为UIImage
- (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }
5.加载启动图的时候隐藏statusbar
在info.plist中加入Status bar is initially hidden 设置为YES
6.获取按钮title的size
/** * 获取按钮title的size */ - (CGFloat)getBtnTitleWidth:(UIButton*)btn { CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}]; return titleSize; }
7.设置Status bar颜色
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, ScreenWidth, 20)];[view setBackgroundColor:COLOR_APP_MAIN]; [viewController.navigationController.navigationBar addSubview:view];
8.json转dictionary,dictionary转json
+ (NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError]; return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; }
+(NSDictionary *)jsonToDic:(NSString*)jsonStr { NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; return dic; }
9.是否允许推送
+(BOOL)isAllowedNotification{ if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; if(UIUserNotificationTypeNone != setting.types) { return YES; } } NSLog(@"不允许推送"); return NO; }
10.磁盘空间相关
+ (NSString *)memoryFormatter:(long long)diskSpace { NSString *formatted; double bytes = 1.0 * diskSpace; double megabytes = bytes / MB; double gigabytes = bytes / GB; if (gigabytes >= 1.0) formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes]; else if (megabytes >= 1.0) formatted = [NSString stringWithFormat:@"%.2f MB", megabytes]; else formatted = [NSString stringWithFormat:@"%.2f bytes", bytes]; NSLog(@"fotmatted=%@",formatted); return formatted; } + (NSString *)totalDiskSpace { long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue]; return [self memoryFormatter:space]; } + (NSString *)freeDiskSpace { long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue]; return [self memoryFormatter:freeSpace]; }
11.修改了leftBarButtonItem如何恢复系统侧滑返回功能
//设置代理 self.interactivePopGestureRecognizer.delegate = self; #pragma mark - <UIGestureRecognizerDelegate> //实现代理方法:return YES :手势有效, NO :手势无效 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { //当导航控制器的子控制器个数 大于1 手势才有效 return self.childViewControllers.count > 1; }
或者用第三方 UINavigationController+FDFullscreenPopGesture
12.使用UIAppearance在某个状态下设置颜色,字体等不好使
只需要在对应的位置用layoutIfNeeded
刷新一下就可以了
13.设置圆形图片
/** 设置圆形图片(放到分类中使用) */ - (UIImage *)cutCircleImage { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); // 获取上下文 CGContextRef ctr = UIGraphicsGetCurrentContext(); // 设置圆形 CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); CGContextAddEllipseInRect(ctr, rect); // 裁剪 CGContextClip(ctr); // 将图片画上去 [self drawInRect:rect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
14.如果在xib中有一个控件, 已经明确设置尺寸了,输出的frame也是对的, 但是显示出来的效果不一样(比如尺寸变大了), 如果是这种情况一般就是autoresizingMask自动伸缩属性在搞鬼!
解决办法如下:
//xib的awakeFromNib方法中设置UIViewAutoresizingNone进行清空 - (void)awakeFromNib { self.autoresizingMask = UIViewAutoresizingNone; }
15.通过图片Data数据第一个字节 来获取图片扩展名
- (NSString *)contentTypeForImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch (c) { case 0xFF: return @"jpeg"; case 0x89: return @"png"; case 0x47: return @"gif"; case 0x49: case 0x4D: return @"tiff"; case 0x52: if ([data length] < 12) { return nil; } NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding]; if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) { return @"webp"; } return nil; } return nil; }
16.用0补全的方法
NSInteger count = 5; //02代表:如果count不足2位 用0在最前面补全(2代表总输出的个数) NSString *string = [NSString stringWithFormat:@"%02zd",count]; //输出结果是: 05 NSLog(@"%@", string);
总结
以上就是这篇文章的全部内容,希望本文中的这些小技巧能给大家开发iOS的时候提供一定的帮助,如果有疑问大家可以留言交流。