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 - 搜索关键字在结果中高亮显示的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch查询结果高亮操作实战

qt高亮关键字

Lucene —— 搜索结果高亮显示

Android 实现搜索关键字高亮显示+ListView的功能

php开发_多关键字,高亮显示

Android搜索结果显示高亮(有数据滑动底部自动刷新)