自定义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;