個人博客: LiCheng的博客
引言:
當我們在做一些購物類APP或者是商品展示類的APP時业栅,我們經(jīng)常會遇到某種商品打折優(yōu)惠或者搶購的場景秒咐。并且為了吸引客戶,商家一般把打折商品的現(xiàn)價和原價同時顯示出來碘裕,進行對比携取,此時就出現(xiàn)了為展示原價的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/