iOS开发中的富文本

时间:2023-03-09 16:42:08
iOS开发中的富文本

1. 改变指定字符的颜色

NSString * text = [NSString stringWithFormat:@"%@米",distance];

NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:text];

[attributedStr addAttribute:NSForegroundColorAttributeName value:blackTextColor range:NSMakeRange(text.length -1,1)];

cell.labelright.attributedText = attributedStr;

2.在价格中间加横线

NSString * text = [NSString stringWithFormat:@"¥ %@",[sourseDic objectForKey:@"originalPrice"]];

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString: text];

[attrStr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, text.length)];

cell.labelOldPrice.attributedText = attrStr;