继承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

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。