美文网首页
IOS手机信息正则判定

IOS手机信息正则判定

作者: 爱你的我13149 | 来源:发表于2017-10-16 08:14 被阅读0次

#import "ViewController.h"#define keyBoardHight 150@interface ViewController ()@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.MessageInputTF.delegate = self;

}

#pragma mark UItextField的代理方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

[UIView animateWithDuration:0.5 animations:^{

self.view.frame = CGRectMake(0, -keyBoardHight, self.view.frame.size.width, self.view.frame.size.height);

}];

NSLog(@"输入框将要开始输入内容");

return YES;

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

NSLog(@"输入框将要结束输入内容");

return YES;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

NSLog(@"输入框将要返回");

[textField resignFirstResponder];

[UIView animateWithDuration:0.5 animations:^{

self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

}];

return YES;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)ClickSendMessageBtn:(id)sender {

NSString *MessageToSendStr = self.MessageInputTF.text;

//获取用户输入的信息,根据邮箱的正则表达式判断用户的信息

NSString *regexStr = @"\\d{5,11}@\\w{0,4}qq.com";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexStr];

NSError *error;

//    NSRegularExpression是用Unicode字符串表达和应用正则表达式的类

/* 匹配是不区分大小写 */

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr

options:NSRegularExpressionCaseInsensitive

error:&error];

NSArray *arrayOfAllMatches = [regex matchesInString:MessageToSendStr options:0 range:NSMakeRange(0, [MessageToSendStr length])];

//NSString *subStr;

NSMutableArray *arr=[[NSMutableArray alloc]init];

NSArray *rangeArr=[[NSMutableArray alloc]init];

NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:MessageToSendStr];

for (NSTextCheckingResult *match in arrayOfAllMatches)

{

NSString* substringForMatch;

substringForMatch = [MessageToSendStr substringWithRange:match.range];

[arr addObject:substringForMatch];

[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:match.range];

}

NSString *subStr = MessageToSendStr;

for (NSString *str in arr)

{

subStr=[subStr  stringByReplacingOccurrencesOfString:str withString:@"邮箱地址"];

}

self.MessageShowLable.attributedText = attributedStr;

//    BOOL ifMatches = [predicate evaluateWithObject:MessageToSendStr];

//    //将Lable的颜色初始化

//    self.MessageShowLable.textColor = [UIColor blackColor];

//    //把是邮箱格式的字符串高亮显示

//    if (ifMatches == YES) {

//        //字符串高亮

//        self.MessageShowLable.textColor = [UIColor redColor];

//    }else

//    {

//        //保持原来的颜色

//

//    }

//    self.MessageShowLable.text = MessageToSendStr;

self.MessageInputTF.text = @"";

}

@end

相关文章

网友评论

      本文标题:IOS手机信息正则判定

      本文链接:https://www.haomeiwen.com/subject/ckohuxtx.html