我是靠谱客的博主 称心钢笔,这篇文章主要介绍iOS开发之常用分类,现在分享给大家,希望可以做个参考。

本文介绍了常用的一些分类.

一.UIColor分类(十六进制转颜色)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** 十六进制转颜色 @param hexStr 十六进制字符串 @return 颜色对象 */ + (instancetype)colorWithHexStr:(NSString *)hexStr { if ([hexStr hasPrefix:@"#"]) { hexStr = [hexStr stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""]; } NSInteger redColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(0, 2)] UTF8String],0,16); NSInteger greenColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(2, 2)] UTF8String],0,16); NSInteger blueColorNum = strtoul([[hexStr substringWithRange:NSMakeRange(4, 2)] UTF8String],0,16); UIColor *color = BYColor(redColorNum, greenColorNum, blueColorNum); return color; }

二.UITextField分类(手机账号和密码验证)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
- (BOOL)isEmptyText { return self.text.length == 0; } /* 中国移动: 135、136、137、138、139、147(数据卡)、148、150、151、152、157、158、159、178、182、183、184、187、188、198、 134(0-8)、1440、1703、1705、1706 中国联通: 130、131、132、145(数据卡)、146、155、156、166、175、176、185、186、 1707、1708、1709 、 中国电信: 133、149、153、173、177、180、181、189、199、 1349、1410、1700、1701、1702、 虛拟运营商: 1700、1705、1707、1708、1709......代理联通移动电信业务 */ // 判断手机号码格式是否正确 - (BOOL)isValidPhoneNumber { // phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; if (self.text.length != 11) { return NO; } else { /** * 移动号段正则表达式 * CM China Mobile */ NSString *CMPhoneSegmentRegExp = @"^1((3[5-9])|(4[7-8])|(5[0-2,7-9])|(78)|(8[2-4,7-8])|(98))\d{8}|((34[0-8])|(440)|(70[3,5-6]))\d{7}$"; /** * 联通号段正则表达式 * CU China Unicom */ NSString *CUPhoneSegmentRegExp = @"^1((13[0-2])|(4[5-6])|(5[5-6])|(66])|(7[5-6])|(8[5,6]))\d{8}|(70[7-9])\d{7}$"; /** * 电信号段正则表达式 * CT China Telecom */ NSString *CTPhoneSegmentRegExp = @"^1((33)|(49)|(53)|(17[3-7])|(18[0,1,9])|(99))\d{8}((349)|(410)|(70[0-2]))\d{7}$"; NSPredicate *predicateCM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CMPhoneSegmentRegExp]; BOOL isMatchCM = [predicateCM evaluateWithObject:self.text]; NSPredicate *predicateCU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CUPhoneSegmentRegExp]; BOOL isMatchCU = [predicateCU evaluateWithObject:self.text]; NSPredicate *predicateCT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CTPhoneSegmentRegExp]; BOOL isMatchCT = [predicateCT evaluateWithObject:self.text]; if (isMatchCM || isMatchCU || isMatchCT) { return YES; } else { return NO; } } } // 判断秘密格式为 6 - 18 位数字和字母组成 - (BOOL)isValidPassword { NSString *regExp = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regExp]; if ([predicate evaluateWithObject:self.text]) { return YES ; } else { return NO; } } // 长度是否超过6位数并小于18位数 - (BOOL)isMoreThanSixLength { return (self.text.length >= 6 && self.text.length <= 18); }

三.NSString分类(缓存路径)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 获取Documents目录 + (NSString *)documentPath { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } // 获取Cache目录 + (NSString *)cachePath { return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; } // 获取Tmp目录 + (NSString *)tempPath { return NSTemporaryDirectory(); } // 获取拼接后的Documents目录 - (NSString *)appendDocumentsPath { NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *fileName = [self lastPathComponent]; NSString *filePath = [documentsPath stringByAppendingPathComponent:fileName]; return filePath; } // 获取拼接后的Cache目录 - (NSString *)appendCachePath { NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *fileName = [self lastPathComponent]; NSString *filePath = [cachePath stringByAppendingPathComponent:fileName]; return filePath; } // 获取拼接后的Tmp目录 - (NSString *)appendTmpPath { NSString *tmpPath = NSTemporaryDirectory(); NSString *fileName = [self lastPathComponent]; NSString *filePath = [tmpPath stringByAppendingPathComponent:fileName]; return filePath; }

后续再更…

最后

以上就是称心钢笔最近收集整理的关于iOS开发之常用分类的全部内容,更多相关iOS开发之常用分类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部