iphone实用技巧二(持续更新)
2014-07-07 15:00
148 查看
// 正则判断手机号码地址格式
- (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}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189
22 */
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// 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;
}
}
//textField字数限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
if ([string isEqualToString:@"\n"])
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (self.nickName == textField)
{
if ([toBeString length] > 12) {
textField.text = [toBeString substringToIndex:12];
[SGInfoAlert showInfo:@" \n超过最大字数不能输入了\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.3];
return NO;
}
}
if (self.address == textField)
{
if ([toBeString length] > 30) {
textField.text = [toBeString substringToIndex:30];
[SGInfoAlert showInfo:@" \n超过最大字数不能输入了\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.3];
return NO;
}
}
return YES;
}
//textView字数限制
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSInteger number ;
number = [textView.text length]+text.length;
if (number > 100) {
[SGInfoAlert showInfo:@" \n最大字数不能超过100\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.8];
textView.text = [NSString stringWithFormat:@"%@%@",textView.text,text];
textView.text = [textView.text substringToIndex:100];
return NO;
}else{
return YES;
}
}
//开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textView
{
NSLog(@"textViewDidBeginEditing");
CGRect frame = textView.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textView
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textViewDidBeginEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing");
CGRect frame = textView.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 320.0);//键盘高度216,这里用320升高点
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textViewDidEndEditing:(UITextView *)textView
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//判断生日是否合法
- (BOOL)isValidateBirthday:(NSString *)birthday
{
NSString *emailCheck = @"[0-9]{4}+[-]+[0-9]{1,2}+[-]+[0-9]{1,2}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailCheck];
return [emailTest evaluateWithObject:birthday];
}
//邮箱是否合法判断
- (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)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}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189
22 */
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// 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;
}
}
//textField字数限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
if ([string isEqualToString:@"\n"])
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (self.nickName == textField)
{
if ([toBeString length] > 12) {
textField.text = [toBeString substringToIndex:12];
[SGInfoAlert showInfo:@" \n超过最大字数不能输入了\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.3];
return NO;
}
}
if (self.address == textField)
{
if ([toBeString length] > 30) {
textField.text = [toBeString substringToIndex:30];
[SGInfoAlert showInfo:@" \n超过最大字数不能输入了\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.3];
return NO;
}
}
return YES;
}
//textView字数限制
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSInteger number ;
number = [textView.text length]+text.length;
if (number > 100) {
[SGInfoAlert showInfo:@" \n最大字数不能超过100\n "
bgColor: [UIColor orangeColor].CGColor
inView:self.view
vertical:0.8];
textView.text = [NSString stringWithFormat:@"%@%@",textView.text,text];
textView.text = [textView.text substringToIndex:100];
return NO;
}else{
return YES;
}
}
//开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textView
{
NSLog(@"textViewDidBeginEditing");
CGRect frame = textView.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textView
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textViewDidBeginEditing:(UITextView *)textView
{
// NSLog(@"textViewDidBeginEditing");
CGRect frame = textView.frame;
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 320.0);//键盘高度216,这里用320升高点
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textViewDidEndEditing:(UITextView *)textView
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
//判断生日是否合法
- (BOOL)isValidateBirthday:(NSString *)birthday
{
NSString *emailCheck = @"[0-9]{4}+[-]+[0-9]{1,2}+[-]+[0-9]{1,2}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailCheck];
return [emailTest evaluateWithObject:birthday];
}
//邮箱是否合法判断
- (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];
}
相关文章推荐
- ios学习--iphone开发笔记和技巧总结(原址持续更新)
- sublime使用技巧(2)-- 实用插件推荐【持续更新】
- Android 开发(实用技巧,持续更新)
- [置顶] Android 实用技巧知识合集(持续更新)
- iphone 开发小技巧(持续更新中)
- 十分钟搞定pandas(增加了几个实用技巧,持续更新)
- [置顶] IntelliJ IDEA 实用技巧(持续更新...)
- ios学习--iphone开发笔记和技巧总结(原址持续更新)
- PPT(PowerPoint2007)制作技巧(持续更新 by 2010.7.28)
- linux shell 操作技巧 (持续更新)
- 编程不得不去的网站实用网站地址(持续更新)
- Linux下的实用工具(持续更新)
- vs编译的一些技巧(持续更新中)
- MySql常用命令及sql 技巧 (持续更新)
- iPhone代码片段收集(持续更新)
- 最近整理电脑硬盘,分享几个小巧实用的软件下载,持续更新
- Altium使用技巧--持续更新
- Ubuntu下一些实用操作(持续更新中...)
- js 实用函数 持续更新-->也许这里有你需要的