您好,登錄后才能下訂單哦!
小編給大家分享一下iOS代碼片段的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
【1】鍵盤遮擋的一種解決方法:
主要實現邏輯: 將需要顯示的控件放置在一個單獨的UIView中,然后整個view上移處理
代碼實現:
+(void)animationView:(UIScrollView *)anmaView activeRect:(CGRect)filedRect
{
// CGRect filedRect=filed.frame;
CGPoint point=anmaView.contentOffset;
// 計算出距離屏幕上方的相對坐標高度
float realDis=filedRect.origin.y-point.y;
// 計算到目標點的距離的絕對值
int distance1=0;
if (iPhone5) {
distance1=120;
}else{
distance1=90;
}
float distance=fabsf(realDis-distance1);
if (realDis>distance1) {
point.y+=distance;
}else{
point.y-=distance;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
anmaView.contentOffset=point;
[UIView commitAnimations];
}
【2】移動號碼驗證(網絡轉載,已驗證)
- (BOOL)isMobileNumber:(NSString*)mobileNum
{
/**
* 手機號碼
* 移動:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 聯通:130,131,132,152,155,156,185,186
* 電信:133,1349,153,180,189
*/
NSString* MOBILE= @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
* 中國移動:China Mobile
* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
NSString* CM= @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
* 中國聯通:China Unicom
* 130,131,132,152,155,156,185,186
*/
NSString* CU= @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
* 中國電信:China Telecom
* 133,1349,153,180,189
*/
NSString* CT= @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
* 大陸地區固話及小靈通
* 區號:010,020,021,022,023,024,025,027,028,029
* 號碼:七位或八位
*/
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];
if(([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
}
【3】郵箱驗證(網絡轉載,未驗證)
-(BOOL)validateEmail:(NSString*)email
{
if((0 != [email rangeOfString:@"@"].length) &&
(0 != [email rangeOfString:@"."].length))
{
NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];
[tmpInvalidMutableCharSet removeCharactersInString:@"_-"];
//使用compare option 來設定比較規則,如
//NSCaseInsensitiveSearch是不區分大小寫
//NSLiteralSearch 進行完全比較,區分大小寫
//NSNumericSearch 只比較定符串的個數,而不比較字符串的字面值
NSRange range1 = [email rangeOfString:@"@"
options:NSCaseInsensitiveSearch];
//取得用戶名部分
NSString* userNameString = [email substringToIndex:range1.location];
NSArray* userNameArray = [userNameString componentsSeparatedByString:@"."];
for(NSString* string in userNameArray)
{
NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];
if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])
return NO;
}
NSString *domainString = [email substringFromIndex:range1.location+1];
NSArray *domainArray = [domainString componentsSeparatedByString:@"."];
for(NSString *string in domainArray)
{
NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];
if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
return NO;
}
return YES;
}
else // no ''@'' or ''.'' present
return NO;
}
BOOL NSStringIsValidEmail(NSString *checkString)
{
NString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = @".+@.+\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
以上是“iOS代碼片段的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。