画一个单实线,方向可以定制

  app中很多地方用到了单实线,有的是横着的,有的是竖着的,偷懒的时候直接用UIView,设置背景色就搞定了。。。不过,心里很是不安&不爽。下边就上代码了。

  SingleLineView.h

 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.m

 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

  使用方法:

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);
    }];

  以上。

posted @ 2016-01-21 11:19  i左撇子  阅读(189)  评论(0编辑  收藏  举报