畫一個單實線,方向能夠定製

  app中不少地方用到了單實線,有的是橫着的,有的是豎着的,偷懶的時候直接用UIView,設置背景色就搞定了。。。不過,內心非常不安&不爽。下邊就上代碼了。app

  SingleLineView.hide

 1 #import <UIKit/UIKit.h>
 2 
 3 // 單實線方向
 4 typedef enum {
 5     // 橫向
 6     SingleLineOrientationVertical,
 7     // 縱向
 8     SingleLineOrientationHorizontal
 9 } SingleLineOrientation;
10 
11 @interface SingleLineView : UIView {
12     CGFloat _lineWidth;
13     UIColor *_lineColor;
14     SingleLineOrientation _lineOrientation;
15 }
16 
17 // 單實線寬度
18 @property (nonatomic) CGFloat lineWidth;
19 // 單實線顏色
20 @property (nonatomic, strong) UIColor *lineColor;
21 // 單實線方向
22 @property (nonatomic) SingleLineOrientation lineOrientation;
23 
24 @end

  SingleLineView.matom

 1 #import "SingleLineView.h"
 2 
 3 @implementation SingleLineView
 4 
 5 - (instancetype)init {
 6     self = [super init];
 7     
 8     // 默認狀況下單實線樣式
 9     if (self) {
10         [self setLineWidth:0.5];
11         [self setLineColor:[UIColor grayColor]];
12         [self setLineOrientation:SingleLineOrientationHorizontal];
13         [self setBackgroundColor:[UIColor clearColor]];
14     }
15     
16     return self;
17 }
18 
19 // Only override drawRect: if you perform custom drawing.
20 // An empty implementation adversely affects performance during animation.
21 - (void)drawRect:(CGRect)rect {
22     CGContextRef context = UIGraphicsGetCurrentContext();
23     CGContextSetLineCap(context, kCGLineCapRound);
24     CGContextSetLineWidth(context, self.lineWidth);
25     CGContextSetAllowsAntialiasing(context, YES);
26     CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
27     CGContextBeginPath(context);
28     
29     CGContextMoveToPoint(context, 0, 0);
30     if (self.lineOrientation == SingleLineOrientationHorizontal) {
31         CGContextAddLineToPoint(context, self.frame.size.width, 0);
32     } else {
33         CGContextAddLineToPoint(context, 0, self.frame.size.height);
34     }
35     
36     CGContextStrokePath(context);
37 }
38 
39 #pragma mark - getters && setters
40 
41 - (CGFloat)lineWidth {
42     return _lineWidth;
43 }
44 
45 - (void)setLineWidth:(CGFloat)lineWidth {
46     _lineWidth = lineWidth;
47     [self setNeedsDisplay];
48 }
49 
50 - (UIColor *)lineColor {
51     return _lineColor;
52 }
53 
54 - (void)setLineColor:(UIColor *)lineColor {
55     _lineColor = lineColor;
56     [self setNeedsDisplay];
57 }
58 
59 - (SingleLineOrientation)lineOrientation {
60     return _lineOrientation;
61 }
62 
63 - (void)setLineOrientation:(SingleLineOrientation)lineOrientation {
64     _lineOrientation = lineOrientation;
65 }
66 
67 @end

  使用方法:spa

self.singleLineTest = [[SingleLineView alloc] init];
//[self.singleLineTest setLineOrientation:SingleLineOrientationVertical];
[self.view addSubview:self.singleLineTest];

[self.singleLineTest mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(50);
        make.right.equalTo(self.view).with.offset(-50);
        make.top.equalTo(self.view).with.offset(200);
        make.height.mas_equalTo(1);
    }];

  以上。code

相關文章
相關標籤/搜索