码迷,mamicode.com
首页 > 移动开发 > 详细

iOS - 搜索关键字在结果中高亮显示

时间:2019-06-25 18:35:01      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ring   view   uicolor   event   style   name   span   att   search   

 

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 - 搜索关键字在结果中高亮显示

标签:ring   view   uicolor   event   style   name   span   att   search   

原文地址:https://www.cnblogs.com/gongyuhonglou/p/11084507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!