ios Lable 添加删除线

本文介绍如何在iOS开发中使用NSAttributedString来实现价格标签的特殊样式,包括设置不同颜色、字体及删除线等效果,并提供了一种通过自定义UILabel子类来绘制删除线的方法。

遇到坑了:

    NSString *goodsPrice = @"230.39";
    NSString *marketPrice = @"299.99";
    NSString* prceString = [NSString stringWithFormat:@"%@ %@",goodsPrice,marketPrice];
    DLog(@"----打印--%@---",prceString);

 NSMutableAttributedString*attributedString = [[NSMutableAttributedString alloc]initWithString:prceString];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(253, 91, 120, 1) range:NSMakeRange(0, goodsPrice.length)];
    
    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[prceString rangeOfString:marketPrice]];
    
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:[prceString rangeOfString:marketPrice]];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:[prceString rangeOfString:marketPrice]];
    [priceLabel setAttributedText:attributedString];

我感觉下面的代码写的没有问题,但是运行起来怎么就不行了呢

真是百思不得姐,然后各种百度:http://stackoverflow.com/questions/43070335/nsstrikethroughstyleattributename-how-to-strike-out-the-string-in-ios-10-3

然后然后...,换了种写法:

    NSString *goodsPrice = @"230.39";
    NSString *marketPrice = @"299.99";
    NSString* prceString = [NSString stringWithFormat:@"%@ %@",goodsPrice,marketPrice];
    DLog(@"----打印--%@---",prceString);
    
    NSMutableAttributedString *attritu = [[NSMutableAttributedString alloc]initWithString:prceString];
    [attritu addAttributes:@{
                             NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),
                             NSForegroundColorAttributeName:
                                 [UIColor lightGrayColor],
                             NSBaselineOffsetAttributeName:
                                 @(0),
                             NSFontAttributeName: [UIFont systemFontOfSize:14]
                             } range:[prceString rangeOfString:marketPrice]];
    priceLabel.attributedText = attritu;

如果上面的问题还有问题,我们还可以用其他方式实现这种效果

新建一个集成 UILabel 的子类,

- (void)drawRect:(CGRect)rect
{
    // 调用super的drawRect:方法,会按照父类绘制label的文字
    [super drawRect:rect];
    
    // 取文字的颜色作为删除线的颜色
    [self.textColor set];
    CGFloat w = rect.size.width;
    CGFloat h = rect.size.height;
    // 绘制(这个数字是为了找到label的中间位置,0.35这个数字是试出来的,如果不在中间可以自己调整)
    UIRectFill(CGRectMake(0, h * 0.5, w, 1));
}

别忘了sizeToFit 不然线会根据空间的 width来画的

    YJlale *priceLabel = [[YJlale alloc] initWithFrame:CGRectMake(20, 20, 200, 20)];
    priceLabel.textColor = [UIColor redColor];
    priceLabel.text = @"1234";
    [priceLabel sizeToFit];
    [self.view addSubview:priceLabel];

 

转载于:https://www.cnblogs.com/ningmengcao-ios/p/6695407.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值