1、在iOS开发中,我们可能会想要这种效果,尤其是商品售价上面:
2、废话不多说直接撸代码:
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 200, 50)];
[self.view addSubview:label]; label.text = @"10.00";
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
// 横线的颜色跟随label字体颜色改变
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",label.text]]; [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
label.attributedText = newPrice;
3、所用知识点:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;