iOS開發(fā)常用功能整理


此文純?yōu)樽约簜渫?,不喜勿噴,默默關(guān)掉!

1.改變狀態(tài)欄顏色

(1)在info.plist文件中改

(2)代碼更改

//設(shè)置狀態(tài)欄顏色

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

(3)可在General中修改

2.設(shè)置Navagation和TabBar


//設(shè)置導(dǎo)航欄背景顏色和字體顏色


[[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];//設(shè)置字體的渲染顏色


3.imageWithColor是顏色轉(zhuǎn)換成image的一個(gè)方法

+ (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.郵箱,手機(jī)號(hào),身份證號(hào)等判斷

//郵箱


+ (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];

}

//手機(jī)號(hào)碼驗(yàn)證

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

//手機(jī)號(hào)以13, 15,18開頭,八個(gè) \d 數(shù)字字符

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];

}

//身份證號(hào)

+ (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];

}

//數(shù)字

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

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

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

return [countTest evaluateWithObject:count];

}

//網(wǎng)址

+ (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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容