首页 > 移动平台 > 详细

iOS 将一串字符里面的某个字符全部标志出来

时间:2016-09-02 17:20:15      阅读:187      评论:0      收藏:0      [点我收藏+]
    NSMutableString * mutStr = [NSMutableString stringWithString:@"aaabbbbaaaccc"];
    NSString * str = @"a";
    NSRange range = {0,mutStr.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range];
    
    NSMutableAttributedString * attr = [[NSMutableAttributedString alloc]initWithString:mutStr];
    NSError * error;
    NSRegularExpression * express = [NSRegularExpression regularExpressionWithPattern:str options:NSRegularExpressionCaseInsensitive error:&error];
    NSArray<NSTextCheckingResult *> * result = [express matchesInString:mutStr options:0 range:range];
    for (NSTextCheckingResult * match in result) {
        NSRange range = [match range];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    }
    
    UILabel * lbl = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    [self.view addSubview:lbl];
    lbl.textColor = [UIColor blueColor];
    lbl.attributedText = attr;

  技术分享

iOS 将一串字符里面的某个字符全部标志出来

原文:http://www.cnblogs.com/tanglimei/p/5834171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!