/// 只要包含有搜索关键字都要高亮
- (void)searchKeyHighlight:(NSString *)key {
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_contenL.text];
NSMutableAttributedString *attriKey = [[NSMutableAttributedString alloc] initWithString:key attributes:@{NSForegroundColorAttributeName: cc_green}];
// 法1
// NSArray <NSString *>*arr = [str.string componentsSeparatedByString:key];
// NSInteger i = 0;
// NSRange range;
// while (i<arr.count-1) {
// if (i>0) {
// range = NSMakeRange(range.location + key.length + arr[i].length, key.length);
// } else {
// range = NSMakeRange(arr[i].length, key.length);
// }
// [str replaceCharactersInRange:range withAttributedString:attriKey];
// i++;
// }
// _contenL.attributedText = str;
// 法2
[str.string enumerateRegexMatches:key options:NSRegularExpressionCaseInsensitive usingBlock:^(NSString * _Nonnull match, NSRange matchRange, BOOL * _Nonnull stop) {
[str replaceCharactersInRange:matchRange withAttributedString:attriKey];
}];
_contenL.attributedText = str;
}
检索关键字并替换全部
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 有时候需要把某个文件夹下的文件里面的字符串内容全部替换成另外的字符串。 工具:sublime 操作步骤: 1、查找...
- 1. 问题背景 在一个字符串中,包含了多个空格,每个空格的位置不同,数目也不同,但是需要替换成其他字符,即使是连续...
- replace使用正则表达式···"aa".replace("a",'b')"ba""aa".replace(/a...
- 这道题有两种解法,一种是创建哈希表,另一种是直接利用JS的indexOf方法 创建哈希表 直接利用indexOf方法