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];
}

 

相关文章:

  • 2021-05-12
  • 2022-12-23
  • 2021-06-20
  • 2021-05-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2021-12-24
  • 2021-07-17
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
相关资源
相似解决方案