您的位置:首页 > 其它

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: