iOS开发常用功能整理


此文纯为自己备忘,不喜勿喷,默默关掉!

1.改变状态栏颜色

(1)在info.plist文件中改

(2)代码更改

//设置状态栏颜色

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

(3)可在General中修改

2.设置Navagation和TabBar


//设置导航栏背景颜色和字体颜色


[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:20]}];


//tabBar的背景颜色

[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];

//[[UITabBar appearance] setOpaque:YES];

//tabBar的字体大小和选中之后的颜色

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateNormal];//字体大小

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];//设置字体的渲染颜色


3.imageWithColor是颜色转换成image的一个方法

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

CGRect rect = CGRectMake( 0.0f, 0.0f, size.width, size.height );

UIGraphicsBeginImageContext( rect.size );

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage * result = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return result;

}


4.邮箱,手机号,身份证号等判断

//邮箱


+ (BOOL) validateEmail:(NSString *)email {

NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}

//手机号码验证

+ (BOOL) validateMobile:(NSString *)mobile {

//手机号以13, 15,18开头,八个 \d 数字字符

NSString *phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobile];

}

//用户(正则判断A-Z,a-z,0-9且6到20位)

+ (BOOL) validateUserName:(NSString *)name {

NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";

NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];

BOOL B = [userNamePredicate evaluateWithObject:name];

return B;

}

//密码

+ (BOOL) validatePassword:(NSString *)passWord {

NSString *passWordRegex = @"^[a-zA-Z0-9]{6,12}+$";

NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];

return [passWordPredicate evaluateWithObject:passWord];

}

//身份证号

+ (BOOL) validateIdentityCard: (NSString *)identityCard {

BOOL flag;

if (identityCard.length <= 0) {

flag = NO;

return flag;

}

NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";

NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

return [identityCardPredicate evaluateWithObject:identityCard];

}

//数字

+ (BOOL) validateCount: (NSString *)count {

NSString *countRegex = @"^[0-9]*$";

NSPredicate *countTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", countRegex];

return [countTest evaluateWithObject:count];

}

//网址

+ (BOOL) validateURL:(NSString *)URL

NSString *URLRegex1 = @"^\\w+((\\-\\w+)|(\\.\\w+))*@[A-Za-z0-9]+((\\.|\\-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$";

NSPredicate *URLTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", URLRegex1];

NSString *URLRegex2 = @"http+:[^\\s]*";

NSPredicate *URLTest2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", URLRegex2];

if ([URLTest1 evaluateWithObject:URL] == YES || [URLTest2 evaluateWithObject:URL] == YES) {

return YES;

}else {

return NO;

}

你可能感兴趣的:(iOS开发常用功能整理)