- (NSMutableAttributedString *)attributedString:(NSString *)string{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:HEXCOLOR(0x999999)}];
//遍历字符串中指定字符的所有位置
NSRange isRange = [attributedString.string rangeOfString:self.keyword];
while (isRange.location != NSNotFound || isRange.length != 0) {
//设置指定字符的属性
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor rgb:BK_Color] range:isRange];
//字体背景颜色
// [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor rgb:BK_Color] range:isRange];
//重设指定字符的位置
NSRange searchIsRange = NSMakeRange(isRange.location + isRange.length, attributedString.string.length - (isRange.location + isRange.length));
isRange = [attributedString.string rangeOfString:self.keyword options:NSCaseInsensitiveSearch range:searchIsRange];
}
return attributedString;
}
来源:oschina
链接:https://my.oschina.net/wayzhu/blog/4284801