https://www.jianshu.com/p/817686897ec1?open_source=weibo_search
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]]
4、GCD代码只执行一次
#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];
14、UILabel设置内边距
子类化UILabel,重写drawTextInRect方法
- (void)drawTextInRect:(CGRect)rect {
// 边距,上左下右
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
15、让手机震动一下
倒入框架
#import
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;
(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.nil、Nil、NULL、NSNull的区别
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