iOS - 搜索关键字在结果中高亮显示
Posted gongyuhonglou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - 搜索关键字在结果中高亮显示相关的知识,希望对你有一定的参考价值。
self.labelContent.attributedText = [self highlightText:self.searchStr inStr:searchModel.eventDesc]; - (NSAttributedString *)highlightText:(NSString *)keyword inStr:(NSString *)str NSRange range = [str rangeOfString:keyword]; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str]; if (range.length > 0) [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; else return [[NSAttributedString alloc]initWithAttributedString:attrStr];
- (NSAttributedString *)highlightText:(NSString *)keyword inStr:(NSString *)str NSRange range = [str rangeOfString:keyword]; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str]; if (range.length > 0) [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; else return [[NSAttributedString alloc]initWithAttributedString:attrStr]; #pragma mark UITableViewDelegate - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath NSString *str = [self.strs objectAtIndex:indexPath.row]; cell.textLabel.attributedText = [self highlightText:self.searchTextField.text inStr:str];
以上是关于iOS - 搜索关键字在结果中高亮显示的主要内容,如果未能解决你的问题,请参考以下文章