边肖想和大家分享一个iOS代码片段的实例分析。相信大部分人还是不太了解。因此,我想分享这篇文章供你参考。希望你看完这篇文章后收获多多。让我们一起来看看。
[1]键盘堵塞的解决方案:
主实现逻辑:将待显示的控件放置在单独的UIView中,然后向上移动整个视图进行处理。
代码实现:
(void)animationviews :(uiscrolview *)anmaView active rect :(CGRect)file drect
{
//CGRect file drect=field . frame;
CGPoint point=anmaview . content offset;
//计算屏幕上方的相对坐标高度。
float real dis=filedrect . origin . y-point . y;
//计算到目标点距离的绝对值。
int distance 1=0;
if(iphone 5){ 0
距离1=120;
}else{
距离1=90;
}
浮动距离=fab SF(real dis-distance 1);
if(realdisdistance 1){ 0
point.y=距离;
}else{
point . y-=距离;
}
[ui view Beginanimations : nil context : nil];
[ui view setanimationduration :0.5];
anmaView.contentOffset=point
[ui view commitations];
}
[2]手机号验证(网络转载,已验证)。
-(BOOL)ismobilenumber :(NSString *)mobile enum
{
/**
*手机号码。
*移动: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}$';
/**
*中国移动:中国移动。
* 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}$';
/**
*中国联通:中国联通。
* 130,131,132,152,155,156,185,186
n
bsp; */
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代码片段的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/39245.html