有时候我们需要给文字添加横线,有两种情况:
第一种是贯穿中间的横线:
横线的颜色和文字的颜色保持一致
_oldPriceLabel.text = @"";
_oldPriceLabel.textColor = [UIColor lightGrayColor];
NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",_oldPriceLabel.text]];
[newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(, newPrice.length)];
_oldPriceLabel.attributedText = newPrice;
效果如图:
第二种是给文字添加下划线:
代码如下:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"忘记密码?"];
NSRange titleRange = {,[title length]};
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
[_forgetBtn setAttributedTitle:title
forState:UIControlStateNormal];
效果如下图: