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

继承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://upload-images.jianshu.io/upload_images/2233013-8586bfc43af10782.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容