#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