forked from pres/PREBorderView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+PREBorderView.m
84 lines (65 loc) · 2.51 KB
/
UIView+PREBorderView.m
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
76
77
78
79
80
81
82
83
84
//
// UIView+PREBorderView.m
// PREBorderViewSample
//
// Created by Paul Steinhilber on 30.06.2013.
// Copyright (c) 2013 Paul Steinhilber. All rights reserved.
//
#import "UIView+PREBorderView.h"
@implementation UIView (PREBorderView)
static UIColor* _defaultBorderColor;
- (void)setDefaultBorderColor:(UIColor *)defaultBorderColor {
_defaultBorderColor = defaultBorderColor;
}
- (UIColor *)defaultBorderColor {
if (!_defaultBorderColor) {
if ([self respondsToSelector:@selector(tintColor)]) {
return self.tintColor;
} else {
return [UIColor blueColor];
}
} else {
return _defaultBorderColor;
}
}
- (void)addOneRetinaPixelBorder {
self.layer.borderWidth = 0.5;
self.layer.borderColor = self.defaultBorderColor.CGColor;
}
- (void)addOneRetinaPixelBorderWithColor:(UIColor*)color {
self.layer.borderWidth = 0.5;
self.layer.borderColor = color.CGColor;
}
- (CALayer *)addOneRetinaPixelLineAtPosistion:(enum PREBorderPosition)position {
return [self addOneRetinaPixelLineWithColor:self.defaultBorderColor atPosistion:position];
}
- (CALayer *)addOneRetinaPixelLineWithColor:(UIColor*)color atPosistion:(enum PREBorderPosition)position {
return [self addLineWithColor:color andWidth:0.5 atPosistion:position];
}
- (CALayer *)addLineWithWidth:(float)pixelWidth atPosistion:(enum PREBorderPosition)position {
return [self addLineWithColor:self.defaultBorderColor andWidth:pixelWidth atPosistion:position];
}
- (CALayer *)addLineWithColor:(UIColor*)color andWidth:(float)pixelWidth atPosistion:(enum PREBorderPosition)position {
if (!([UIScreen mainScreen].scale == 2) && pixelWidth<1) {
pixelWidth = 1;
}
CALayer *border = [CALayer layer];
switch (position) {
case PREBorderPositionTop:
border.frame = CGRectMake(0, 0, self.frame.size.width, pixelWidth);
break;
case PREBorderPositionBottom:
border.frame = CGRectMake(0, self.frame.size.height-pixelWidth, self.frame.size.width, pixelWidth);
break;
case PREBorderPositionLeft:
border.frame = CGRectMake(0, 0, pixelWidth, self.frame.size.height);
break;
case PREBorderPositionRight:
border.frame = CGRectMake(self.frame.size.width-pixelWidth, 0, pixelWidth, self.frame.size.height);
break;
}
border.backgroundColor = color.CGColor;
[self.layer addSublayer:border];
return border;
}
@end