美文网首页
iOS 文字实现高亮

iOS 文字实现高亮

作者: kendi_66 | 来源:发表于2022-03-15 17:19 被阅读0次

/**正则匹配关键字高亮

keyword就是需要高亮的关键字

options:NSRegularExpressionCaseInsensitive 不区分大小写

*/

NSMutableAttributedString* newString = [[NSMutableAttributedString alloc] initWithString:model.totalstring];

NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:[NSString stringWithFormat:@"%@",keyWord] options:NSRegularExpressionCaseInsensitive error:nil];

[regex enumerateMatchesInString:model.totalstring options:NSMatchingReportProgress range:NSMakeRange(0, [model.totalstring length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL stop) {
[newString addAttribute:(NSString
)NSForegroundColorAttributeName
value:(id)[UIColor redColor]
range:result.range];
} ];

相关文章

网友评论

      本文标题:iOS 文字实现高亮

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