关键字高亮

来源:互联网 发布:长绸扇子淘宝 编辑:程序博客网 时间:2024/04/30 12:56
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *reuse = @"reuse";    LGSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];    if (!cell) {        cell = [[[LGSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];    }    LGModel *model = self.listArr[indexPath.row];    //  设置标签文字    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:model.name];    //  获取标红的位置和长度    NSRange range = [model.name rangeOfString:self.keyWord];    //  设置标签文字的属性    [attributeString setAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:range];    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.imgurl] placeholderImage:PLACEHOLDERIMAGE];    cell.nameLabel.text = model.name;    //  高亮红字显示在nameLabel中    cell.nameLabel.attributedText = attributeString;    cell.levelLabel.text = model.level;    cell.priceLabel.text = model.pricebetween;    return cell;}
0 0
原创粉丝点击