美文网首页
IOS正则邮箱地址判定

IOS正则邮箱地址判定

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

#import "ViewController.h"@interface ViewController ()@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.MessageImport.delegate = self;

}

#pragma mark UItextField的代理方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

[UIView animateWithDuration:0.5 animations:^{

self.view.frame = CGRectMake(0, -150, 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;

}

- (IBAction)ClickUploadingBtn:(id)sender {

NSString *MessageToSendStr = self.MessageImport.text;

//获取用户输入的信息

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

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

NSError *error;

//匹配是不区分大小写

NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regexStr

options:NSRegularExpressionCaseInsensitive

error:&error];

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

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.MessageShow.attributedText = attributedStr;

self.MessageImport.text = @"";

}

@end

相关文章

网友评论

      本文标题:IOS正则邮箱地址判定

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