美文网首页
让UILabel自带中划线,当要显示打折价格时

让UILabel自带中划线,当要显示打折价格时

作者: EI_Rey | 来源:发表于2018-11-02 10:49 被阅读10次

继承UILabel

.h

@interface LineLabel : UILabel

@property(nonatomic, assign) BOOL showLine;
@property(nonatomic, assign) UIColor * showLineColor;

@end

.m

#import "LineLabel.h"
@implementation LineLabel

//添加中划线或者是下划线或者任意位置的横线(自己调整)
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (_showLine) {
        if (_showLineColor) {
            [_showLineColor set];  //横线的颜色设置
        }else {
            [[UIColor blackColor] set]; //默认 横线颜色
        }
        
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(c, 1);
        CGContextBeginPath(c);
        CGFloat halfWayUp = rect.size.height/2 + rect.origin.y;
        CGContextMoveToPoint(c, rect.origin.x, halfWayUp);//起点
        CGContextAddLineToPoint(c, rect.origin.x + rect.size.width, halfWayUp);//终点
        CGContextStrokePath(c);
    }
}


@end
![IMG_0219.PNG](https://img.haomeiwen.com/i2233013/8586bfc43af10782.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:让UILabel自带中划线,当要显示打折价格时

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