个人博客: LiCheng的博客
引言:
当我们在做一些购物类APP或者是商品展示类的APP时,我们经常会遇到某种商品打折优惠或者抢购的场景。并且为了吸引客户,商家一般把打折商品的现价和原价同时显示出来,进行对比,此时就出现了为展示原价的label添加中划线或者是下划线的需求。
我们参考一下驴妈妈的首页:
下面就直接上代码了:
添加中划线:
UILabel * strikeLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))];
NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost];
//中划线
NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];
// 赋值
strikeLabel.attributedText = attribtStr;
[self.view addSubview:strikeLabel];
添加下划线:
UILabel *underlineLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 50, 30))];
NSString *textStr = [NSString stringWithFormat:@"%@元", primeCost];
// 下划线
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:textStr attributes:attribtDic];
//赋值
underlineLabel.attributedText = attribtStr;
[self.view addSubview:underlineLabel];
效果如图所示:
GitHub: https://github.com/LiCheng244/LCUtils
个人博客: http://www.licheng244.com/