1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// KWMLineView.m
// iOrangeBusiness
//
// Created by Yaotian on 7/27/15.
// Copyright (c) 2015 kwm. All rights reserved.
//
#import "KWMLineView.h"
#import "UIColor+SAMAdditions.h"
@interface KWMLineView ()
@property (nonatomic) CGFloat thickness;
@end
@implementation KWMLineView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setup];
}
return self;
}
- (void)setup {
self.backgroundColor = [UIColor clearColor];
}
+ (CGFloat)getLineHeight {
return 1.0f/[UIScreen mainScreen].scale;
}
- (CGFloat)thickness {
if (_thickness <= 0) {
_thickness = 1.0f/[UIScreen mainScreen].scale;
}
return _thickness;
}
- (UIColor *)lineColor {
if (_lineColor == nil) {
_lineColor = LINE_COLOR;
}
return _lineColor;
}
- (void)drawRect:(CGRect)rect {
if (self.frame.size.height <= 0) {
CGRect f = self.frame;
f.size.height = [KWMLineView getLineHeight];
self.frame = f;
}
CGContextRef cx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(cx, self.thickness);
CGContextSetStrokeColorWithColor(cx, self.lineColor.CGColor);
CGFloat offset = self.isBottomLine ? self.frame.size.height - self.thickness*0.5 : self.thickness*0.5;
CGContextMoveToPoint(cx, 0, offset);
CGContextAddLineToPoint(cx, self.bounds.size.width, offset);
CGContextStrokePath(cx);
}
@end