IOS正则邮箱地址判定

#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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容