zoukankan      html  css  js  c++  java
  • ios Lable 添加删除线

    遇到坑了:

        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];

  • 相关阅读:
    scrum
    control.begininvoke
    ChangeBrowsePosition Method
    常见linux命令(表格分类)
    Python 之优先级排序
    Python 之分辨双胞胎:copy(浅拷贝)与 deepcopy(深拷贝)
    字符编码学习总结
    Python 多继承方式及顺序
    AttributeError: module 'datetime' has no attribute 'now' ------解决方法之一
    Python 模块定义、导入、优化详解
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/6695407.html
Copyright © 2011-2022 走看看