在做一些网购类APP或者是商品展示类的APP时,我们经常会遇到某种商品打折优惠或者抢购的场景,并且为了吸引客户,商家一般把打折商品的现价和原价同时显示出来,进行对比,此时就出现了为展示原价的label添加中划线或者是下划线的需求。下面就直接上代码了:
1、添加中划线。
UILabel*oldPriceLabel = (UILabel*)[selfviewWithTag:70+i];
NSString*oldStr = [NSStringstringWithFormat:@"%ld元",oldPrice];
//中划线
NSDictionary*attribtDic =@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString*attribtStr = [[NSMutableAttributedString alloc]initWithString:oldStr attributes:attribtDic];
oldPriceLabel.attributedText= attribtStr;
效果如图所示:
2、添加下划线。
UILabel*oldPriceLabel = (UILabel*)[selfviewWithTag:70+i];
NSString*oldStr = [NSStringstringWithFormat:@"%ld元",oldPrice];
//下划线
NSDictionary*attribtDic =@{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString*attribtStr = [[NSMutableAttributedString alloc]initWithString:oldStr attributes:attribtDic];
oldPriceLabel.attributedText= attribtStr;
效果如图所示: