有时候我们需要给文字添加横线,有两种情况:
第一种是贯穿中间的横线:
横线的颜色和文字的颜色保持一致
_oldPriceLabel.text = @"3500"; _oldPriceLabel.textColor = [UIColor lightGrayColor]; NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",_oldPriceLabel.text]]; [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)]; _oldPriceLabel.attributedText = newPrice;
效果如图:

第二种是给文字添加下划线:
代码如下:
NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"忘记密码?"]; NSRange titleRange = {0,[title length]}; [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange]; [_forgetBtn setAttributedTitle:title forState:UIControlStateNormal];
效果如下图:

本文介绍如何在iOS应用中使用NSAttributedString为文本添加贯穿中间的横线及下划线效果,包括具体实现代码及其展示效果。
1万+

被折叠的 条评论
为什么被折叠?



