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

网友评论