美文网首页
正则表达式在textView里运用!

正则表达式在textView里运用!

作者: hunterzhu | 来源:发表于2016-08-28 19:28 被阅读346次

    今天我们来学习一些简单的正则表达式在app的运用。众所周知,正则表达式就是一个文字的检索,其中很多的符号,然后又这些东西去查找到我们需要的东西。
    上代码:

    #import "ViewController.h"
    #import "RegexKitLite.h"
    @interface ViewController ()<UITextViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //很多的文字
        NSString *str =@"萝莉少女人妻大娘组合!!治愈的一脸血!!!//@Tacke竹桑 加班中发来贺电!萝莉少女人妻大娘组合祝大家生蛋圆蛋快乐!!^O^!#圣诞快乐#《We Wish You A Merry Christmas》http://t.cn/8k8UAe7 唱:烟花爆竹白馒头 @重小烟的目标是开机学习关机做题 的@Tacke竹桑 @馒头妞OvO 后期:duoduo 海报:@呆魚胖成狗求拯救QAQ 拿到歌真的好惊喜!总算是赶在圣诞夜发歌辣,祝大家生蛋快乐~ @原创音乐基地 FuelWeb(http://t.cn/8kQNxap)已经默认支持中/英切换啦,目前还有部分未翻译成中文,大家对翻译有任何的意见,请发至zhong.wenjia@99cloud.net,我们会及时更新。@ben_杜玉杰 @ben_杜玉杰 @qyjohn_ @Ada李力 @马沛 @爱开源的贡献开源社区 @-钟文佳- #OpenStack# " ;
        //热点检索
        NSString *hot = @"#[^#]+#";
        //@人,用户名是有2-30位数+空格,除却邮箱的一种方法
        NSString *aite = @"@[\\w-]{2,30}\\s";
        //链接
        NSString *link = @"http(s)?://([0-9a-zA-Z._-]+(/)?)*";
      
        //文字拼接,将你需要检索的放在一个字符串里面
        NSString *manyStr = [NSString stringWithFormat:@"%@|%@|%@",hot,aite,link];
        
        //添加一个textView
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        
        //设置可变的属性字符串
        NSMutableAttributedString *attrubutedStr = [[NSMutableAttributedString alloc] initWithString:str];
        
        //系统自带
    //    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:manyStr options:NSRegularExpressionCaseInsensitive error:nil];
    //    NSArray *regArr = [regex matchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
    //    
    //    for (NSTextCheckingResult *result in regArr) {
    //        NSString *cule = [str substringWithRange:result.range];
    //        
    //        NSRange range1 = [str rangeOfString:cule];
    //        //设置属性
    //        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    //        
    //        NSDictionary *dic = @{
    //                              NSFontAttributeName : [UIFont systemFontOfSize:20],
    //                              NSForegroundColorAttributeName:[UIColor orangeColor],
    //                              };
    //        [attrubutedStr addAttribute:NSLinkAttributeName value:url range:range1];
    //        [attrubutedStr addAttributes: dic range:range1];
    //        NSLog(@"%@",cule);
    //        
    //    }
        //利用框架
        NSArray *regexArr = [str componentsMatchedByRegex:manyStr];
        //遍历
        for (NSString *string in regexArr) {
            //范围
            NSRange range1 = [str rangeOfString:string];
            //设置属性
            //这里添加了一个链接,百度
            NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
            NSDictionary *dic = @{
                                  NSFontAttributeName : [UIFont systemFontOfSize:15],
                                  NSForegroundColorAttributeName:[UIColor colorWithRed:0.7 green:0.1 blue:0.1 alpha:1],
                                  };
            [attrubutedStr addAttribute:NSLinkAttributeName value:url range:range1];
            [attrubutedStr addAttributes: dic range:range1];
    
        }
        //关闭编辑
        textView.editable = NO;
        //将属性文字添加上去
        textView.attributedText = attrubutedStr;
        
        [self.view addSubview:textView];
    }
    
    屏幕快照 2016-08-28 下午7.26.33.png

    相关文章

      网友评论

          本文标题:正则表达式在textView里运用!

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