iOS有时候会用到的小技巧

iOS有时候会用到的小技巧记录文档

获取某个view所在的控制器
1
2
3
4
5
6
7
8
9
10
11
12
13
- (UIViewController *)viewController
{
UIViewController *viewController = nil;
UIResponder *next = self.nextResponder;
while (next) {
if ([nextisKindOfClass:[UIViewController class]]) {
viewController = (UIViewController *)next;
break;
}
next = next.nextResponder;
}
return viewController;
}
iOS跳转到App Store下载应用评分
1
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APPID"]];
iOS 获取汉字的拼音
1
2
3
4
5
6
7
8
9
10
11
12
+ (NSString *)transform:(NSString *)chinese {    
//将NSString装换成NSMutableString
NSMutableString *pinyin = [chinese mutableCopy];
//将汉字转换为拼音(带音标)
CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
NSLog(@"%@", pinyin);
//去掉拼音的音标
CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
//返回最近结果
return pinyin;
}
判断view是不是指定视图的子视图
1
BOOL isView = [textView isDescendantOfView:self.view];
修改UITextField中Placeholder的文字颜色和大小
1
2
3
[self.textFieldsetValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];

[self.textFieldsetValue:[UIFontboldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
NSDateFormatter的格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
G: 公元时代,例如AD公元
yy: 年的后2
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
K:时,12小时制,0-11
m: 分,1-2
mm: 分,2
s: 秒,1-2
ss: 秒,2
S: 毫秒
验证移动手机号:
1
2
3
4
5
6
+ (BOOL)isMobile:(NSString*)mobile
{
NSString *regex = @"^1+[34578]+\d{9}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:mobile];
}
邮箱验证:
1
2
3
4
5
6
+ (BOOL)isEmail:(NSString*)email
{
NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:email];
}
整形验证
1
2
3
4
5
+ (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
浮点型验证
1
2
3
4
5
- (BOOL)isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
中文验证
1
2
3
4
5
6
+ (BOOL)isChinese:(NSString*)chinese
{
NSString *regex = @"^[\u4e00-\u9fa5]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject: chinese];
}
网址URL验证
1
2
3
4
5
6
+ (BOOL)isURL:(NSString*)url
{
NSString *regex = @"[a-zA-z]+://[^\s]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject: url];
}
身份证号验证
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
+ (BOOL)verifyIDCardNumber:(NSString *)IDCardNumber 
{
IDCardNumber = [IDCardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([IDCardNumber length] != 18)
{
return NO;
}
NSString *mmdd = @”(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))”;
NSString *leapMmdd = @”0229”;
NSString *year = @”(19|20)[0-9]{2}”;
NSString *leapYear = @”(19|20)(0[48]|[2468][048]|[13579][26])”;
NSString *yearMmdd = [NSString stringWithFormat:@”%@%@”, year, mmdd];
NSString *leapyearMmdd = [NSString stringWithFormat:@”%@%@”, leapYear, leapMmdd];
NSString *yyyyMmdd = [NSString stringWithFormat:@”((%@)|(%@)|(%@))”, yearMmdd, leapyearMmdd, @”20000229”];
NSString *area = @”(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}”;
NSString *regex = [NSString stringWithFormat:@”%@%@%@”, area, yyyyMmdd , @”[0-9]{3}[0-9Xx]”];

NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if (![regexTest evaluateWithObject:IDCardNumber])
{
return NO;
}
int summary = ([IDCardNumber substringWithRange:NSMakeRange(0,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([IDCardNumber substringWithRange:NSMakeRange(1,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([IDCardNumber substringWithRange:NSMakeRange(2,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([IDCardNumber substringWithRange:NSMakeRange(3,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([IDCardNumber substringWithRange:NSMakeRange(4,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([IDCardNumber substringWithRange:NSMakeRange(5,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([IDCardNumber substringWithRange:NSMakeRange(6,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(16,1)].intValue) *2
+ [IDCardNumber substringWithRange:NSMakeRange(7,1)].intValue *1 + [IDCardNumber substringWithRange:NSMakeRange(8,1)].intValue *6
+ [IDCardNumber substringWithRange:NSMakeRange(9,1)].intValue *3;
NSInteger remainder = summary % 11;
NSString *checkBit = @"";
NSString *checkString = @"10X98765432";
checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
return [checkBit isEqualToString:[[IDCardNumber substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}
一般用户名的验证
1
2
3
4
5
6
+ (BOOL) isUserName:(NSString *)name
{
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
return [userNamePredicate evaluateWithObject:name];
}
像safari一样滑动的时候隐藏navigationbar
1
self.navigationController.hidesBarsOnSwipe = YES;
去掉plan模式下tableView尾部的多余横线
1
self.tableView.tableFooterView = [UIView new];
设置导航透明 和 隐藏导航下面的线条
1
2
3
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.shadowImage = [UIImage new];
字符编码介绍 正则表达式

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×