天天看点

UILabel添加删除线

效果图:

UILabel添加删除线

在制作价格时候时候,会用到打折的情况,这种删除线又是如何加上去的呢?

代码:

- (NSMutableAttributedString *)priceStr:(NSString *)priceStr payPrice:(NSString *)payPrice befforRange:(NSRange)befforRange payPriceAfter:(NSString *)payPriceAfter afterRange:(NSRange)afterRange {

    NSMutableAttributedString *pricePay = [[NSMutableAttributedStringalloc]initWithString:priceStr];

    [pricePay addAttributes:@{NSForegroundColorAttributeName:THEMECOLOR,NSFontAttributeName:Font(15)}range:befforRange];

    //删除线

    NSNumber *nuber = [[NSNumberalloc]initWithLong:(NSUnderlinePatternSolid |NSUnderlineStyleSingle)];

    [pricePay addAttribute:NSStrikethroughStyleAttributeNamevalue:nuber range:afterRange];

    return pricePay;

}

//数据改成自己需要的数据即可

 NSString *payPrice = [NSString stringWithFormat:@"%ld",(long)payInfo.onepaymoney_subsidy];

    NSString *payPriceAfter = [NSString stringWithFormat:@"%ld",(long)payInfo.onepaymoney];

    NSString *priceStr =[NSString stringWithFormat:@"已付金额:%@ 元 (原价:%@ 元)",payPrice,payPriceAfter];

    lbFirstPay.attributedText = [self priceStr:priceStr payPrice:payPrice befforRange:NSMakeRange(5, payPrice.length) payPriceAfter:payPriceAfter afterRange:NSMakeRange(5+payPrice.length+7, payPriceAfter.length+1)];

* 主要用到了NSMutableAttributeString这个类,具体的其他更多方法参考官方文档。