繼承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)
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者