1 去除数组中重复的对象
NSArray *newArr = [oldArr valueForKeyPath:@@distinctUnionOfObjects.self"];
2 强 / 弱引用
- #define WeakSelf(type) __weak typeof(type) weak##type = type; // weak
- #define StrongSelf(type) __strong typeof(type) type = weak##type; // strong
3 获取图片资源
#define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
4GCD 代码只执行一次
#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
5 自定义 NSLog
- #ifdef DEBUG
- #define NSLog(fmt, ...) NSLog((@"%s [Line %d]" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
- #else
- #define NSLog(...)
- #endif
6 在主线程上运行
#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
7 开启异步线程
#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
8 通知
- #define NOTIF_ADD(n, f) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(f) name:n object:nil]
- #define NOTIF_POST(n, o) [[NSNotificationCenter defaultCenter] postNotificationName:n object:o]
- #define NOTIF_REMV() [[NSNotificationCenter defaultCenter] removeObserver:self]
9 修改 textField 的 placeholder 的字体颜色大小
- [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
- [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
10 身份证号验证
- - (BOOL)validateIdentityCard {
- BOOL flag;
- if (self.length <= 0) {
- flag = NO;
- return flag;
- }
- NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
- NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
- return [identityCardPredicate evaluateWithObject:self];
- }
11 移除字符串中的空格和换行
- + (NSString *)removeSpaceAndNewline:(NSString *)str {
- NSString *temp = [str stringByReplacingOccurrencesOfString:@"" withString:@""];
- temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
- temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- return temp;
- }
12 字符串是否为空
- + (BOOL)isEqualToNil:(NSString *)str {
- return str.length <= 0 || [str isEqualToString:@""] || !str;
- }
13 将 tableView 滚动到顶部
[tableView setContentOffset:CGPointZero animated:YES];
或者
[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
14UILabel 设置内边距
子类化 UILabel, 重写 drawTextInRect 方法
- - (void)drawTextInRect:(CGRect)rect {
- // 边距, 上左下右
- UIEdgeInsets insets = {0, 5, 0, 5};
- [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
- }
15 让手机震动一下
倒入框架
- #import <AudioToolbox/AudioToolbox.h>
- AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
或者
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
16 在 image 上绘制文字并生成新的 image
- UIFont *font = [UIFont boldSystemFontOfSize:12];
- UIGraphicsBeginImageContext(image.size);
- [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
- CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
- [[UIColor whiteColor] set];
- [text drawInRect:CGRectIntegral(rect) withFont:font];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
17 保存 UIImage 到本地
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
- [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
18 通知监听 APP 生命周期
UIApplicationDidEnterBackgroundNotification 应用程序进入后台
UIApplicationWillEnterForegroundNotification 应用程序将要进入前台
UIApplicationDidFinishLaunchingNotification 应用程序完成启动
UIApplicationDidFinishLaunchingNotification 应用程序由挂起变的活跃
UIApplicationWillResignActiveNotification 应用程序挂起(有电话进来或者锁屏)
UIApplicationDidReceiveMemoryWarningNotification 应用程序收到内存警告
UIApplicationDidReceiveMemoryWarningNotification 应用程序终止(后台杀死手机关机等)
UIApplicationSignificantTimeChangeNotification 当有重大时间改变(凌晨 0 点, 设备时间被修改, 时区改变等)
UIApplicationWillChangeStatusBarOrientationNotification 设备方向将要改变
UIApplicationDidChangeStatusBarOrientationNotification 设备方向改变
UIApplicationWillChangeStatusBarFrameNotification 设备状态栏 frame 将要改变
UIApplicationDidChangeStatusBarFrameNotification 设备状态栏 frame 改变
UIApplicationBackgroundRefreshStatusDidChangeNotification 应用程序在后台下载内容的状态发生变化
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件被锁定, 无法访问
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件可用了
19.NSString 使用 stringWithFormat 拼接的相关知识
保留 2 位小数点
- //.2 代表小数点后面保留 2 位(2 代表保留的数量)
- NSString *string = [NSString stringWithFormat:@"%.2f",M_PI];
- // 输出结果是: 3.14
- NSLog(@"%@", string);
用 0 补全的方法
- NSInteger count = 5;
- //02 代表: 如果 count 不足 2 位 用 0 在最前面补全(2 代表总输出的个数)
- NSString *string = [NSString stringWithFormat:@"%02zd",count];
- // 输出结果是: 05
- NSLog(@"%@", string);
字符串中有特殊符号 % 怎么办
- NSInteger count = 50;
- //% 是一个特殊符号 如果在 NSString 中用到 % 需要如下写法
- NSString *string = [NSString stringWithFormat:@"%zd%%",count];
- // 输出结果是: 50%
- NSLog(@"%@", string);
字符串中有特殊符号 " 怎么办
- NSInteger count = 50;
- //"是一个特殊符号, 如果在 NSString 中用到" 需要用 \ 进行转义
- NSString *string = [NSString stringWithFormat:@"%zd\"",count];
- // 输出结果是: 50"NSLog(@"%@", string);
20.Button 禁止触摸事件的 2 种方式
- // 会改变按钮的状态, 颜色会变灰
- button.enabled = NO;```
但是又有一个需求是既不能点击也不要改变 Button 颜色:
- // 保持按钮原来的状态, 颜色不会变
- button.userInteractionEnabled = NO;
21. 设置圆形图片
- (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;
- }
21.nilNilNULLNSNull 的区别
nil: 指向一个对象的空指针, 对 objective c id 对象赋空值.
Nil: 指向一个类的空指针, 表示对类进行赋空值.
NULL: 指向其他类型 (如: 基本类型 C 类型) 的空指针, 用于对非对象指针赋空值.
NSNull: 在集合对象中, 表示空值的对象.
22. 关闭 UITableViewCell 的点击事件方法:
cell.selectionStyle = UITableViewCellSelectionStyleNone; // 这个属性在 cellForRowAtIndexPath: 方法里面写;
23. 关闭 UITableViewCell 被点击之后的灰色背景效果:
[tableView deselectRowAtIndexPath:indexPath animated:YES]; // 这个属性在 didSelectRowAtIndexPath: 方法里面写;
24. 去掉 UITableViewCell 之间的线条的方法:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 这个方式去掉全部的 cell 线条, 如果想去掉某一条需要自定义 cell 的线条
25. 清空空白 UITableViewCell 的方法:
self.tableView.tableFooterView = [[UIView alloc] init]; 或者 self.tableView.tableFooterView = UIView.new;
参考文章
- https://www.jianshu.com/p/1ff9e44ccc78
- https://www.jianshu.com/p/9fcd37c0ea05
- https://www.jianshu.com/p/8207621ddcaa
- http://blog.csdn.net/CC1991_/article/details/53994578
来源: http://blog.csdn.net/z979451341/article/details/79475460