美文网首页
iOS设置UILabel设置删除线的两种方法

iOS设置UILabel设置删除线的两种方法

作者: lance017 | 来源:发表于2016-08-04 11:30 被阅读697次

自定义Label 使用 drawRect

正常label使用,在给label赋值文字之后调用sizeToFit

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

2017-03-30更新,升级了10.3之后,下面的方法失效了!!!

使用 NSAttributedString

NSAttributedString *attrStr = [[NSAttributedString alloc]
                                   initWithString:_model.originPrice
                                   attributes:@{
                                                NSFontAttributeName:[UIFont systemFontOfSize:20.f],
                                                NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#5bcec0"],
                                                NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid),
                                                NSStrikethroughColorAttributeName:[UIColor colorWithHexString:@"#5bcec0"]}];
self.orginPriceLabel.attributedText = attrStr;

相关文章

网友评论

      本文标题:iOS设置UILabel设置删除线的两种方法

      本文链接:https://www.haomeiwen.com/subject/qaucsttx.html