在做一些網(wǎng)購(gòu)類APP或者是商品展示類的APP時(shí)梗肝,我們經(jīng)常會(huì)遇到某種商品打折優(yōu)惠或者搶購(gòu)的場(chǎng)景姐叁,并且為了吸引客戶,商家一般把打折商品的現(xiàn)價(jià)和原價(jià)同時(shí)顯示出來(lái)墓懂,進(jìn)行對(duì)比焰宣,此時(shí)就出現(xiàn)了為展示原價(jià)的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;
效果如圖所示: