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

上一篇:初次改app


下一篇:使用create-react-app创建应用并执行npm start时出现events.js:183 throw er的解决方法