iOS 实现搜索关键字高亮

 

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

keyword就是须要高亮的关键字orm

options:NSRegularExpressionCaseInsensitive 不区分大小写string

*/it

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

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

[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];
} ];model

相关文章
相关标签/搜索