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

相關文章
相關標籤/搜索