iOS开发设置tableview搜索的关键字高亮

释放双眼,带上耳机,听听看~!
//cellforrow方法中
if (self.searchController.active) {
self.selectForwardCell.selectForwardModel = self.searchList[indexPath.row];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.selectForwardCell.selectForwardModel.name];
UIColor *highlightedColor = [UIColor colorWithRed:0 green:131/255.0 blue:0 alpha:1.0];
NSArray *strArr = [self rangeOfSubString:self.searchString inString:self.selectForwardCell.selectForwardModel.name];
for (NSValue *value in strArr) {
NSRange range = [value rangeValue];
[attributedString addAttribute:NSForegroundColorAttributeName value:highlightedColor range:range];
}
self.selectForwardCell.titleLable.attributedText = attributedString;
}else{
self.selectForwardCell.selectForwardModel = self.dataSource[indexPath.row];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.selectForwardCell.selectForwardModel.name];
self.selectForwardCell.titleLable.attributedText = attributedString;
}
//循环查找相似的搜索关键字
- (NSArray*)rangeOfSubString:(NSString*)subStr inString:(NSString*)string {
NSMutableArray *rangeArray = [NSMutableArray array];
NSString*string1 = [string stringByAppendingString:subStr];
NSString *temp;
for(int i =0; i < string.length; i ++) {
temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];
if ([temp isEqualToString:subStr]) {
NSRange range = {i,subStr.length};
[rangeArray addObject: [NSValue valueWithRange:range]];
}
}
return rangeArray;
}

人已赞赏
iOS文章

iOS开发copy-strong的详解

2020-1-28 9:11:05

iOS文章

iOS开发UITableview 刷新指定的某一个section或cell

2020-1-28 11:35:15

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索