调用下面的方法得到的结果再赋值给控件的attributedString
- (NSAttributedString *)attributeStringWithString:(NSString *)string keyword:(NSString *)keyword
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:keyword options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSMutableAttributedString *attributeString = [[[NSMutableAttributedString alloc] initWithString:string] autorelease];
[attributeString beginEditing];
for (NSTextCheckingResult *result in matches)
{
NSRange range = [result range];
[attributeString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:range];
}
[attributeString endEditing];
return attributeString;
}