NSString设置关键字颜色 凸显关键字

来源:互联网 发布:图片编辑视频软件 编辑:程序博客网 时间:2024/05/08 14:40

直接代码

    NSString * str=@"fjsalfasdfdasfasdjf我很好";        NSMutableAttributedString * attriStr=[[NSMutableAttributedString alloc]initWithString:str];    NSRange range = [str rangeOfString:@"我很好"];    [attriStr addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:range];     UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];    label.attributedText=attriStr;    [self.view addSubview:label];    

好出来效果是


多个keyword用这个方法

<pre name="code" class="objc">        NSError *error;        NSString *strRegex = keyWord;        NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:strRegex options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:&error];                //无视大小写.        NSArray *matches =[reg matchesInString:tempStr options:NSMatchingReportProgress range:NSMakeRange(0, tempStr.length)];                for (NSTextCheckingResult *match in matches)        {            NSRange range = [match range];            [tempAttriStr addAttribute:NSForegroundColorAttributeName value:[UMComTools colorWithHexString:FontColorBlue] range:range];        }


1 0
原创粉丝点击