-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCustomIconButton.m
170 lines (134 loc) · 4.33 KB
/
IRCustomIconButton.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// IRCustomIconButton.m
// IRPasscode
//
// Created by Phil on 2019/11/14.
// Copyright © 2019 Phil. All rights reserved.
//
#import "IRCustomIconButton.h"
@implementation IRCustomIconButton
@synthesize iconSizePersent = _iconSizePersent;
@synthesize iconContentMode = _iconContentMode;
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]){
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]){
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
return self;
}
- (UIViewContentMode)imageViewContentMode {
return self.imageView.contentMode;
}
- (void)setImageViewContentMode:(UIViewContentMode)imageViewContentMode {
self.imageView.contentMode = imageViewContentMode;
[self.imageView invalidateIntrinsicContentSize];
[self.imageView setNeedsLayout];
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
- (NSInteger)iconContentMode {
return _iconContentMode;
}
- (void)setIconContentMode:(NSInteger)iconContentMode {
_iconContentMode = iconContentMode;
[self.imageView invalidateIntrinsicContentSize];
[self.imageView setNeedsLayout];
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
- (CGSize)iconSizePersent {
return _iconSizePersent;
}
- (void)setIconSizePersent:(CGSize)iconSizePersent {
_iconSizePersent = iconSizePersent;
if(_iconSizePersent.width < 0){
_iconSizePersent.width = 0;
}
if(_iconSizePersent.height < 0){
_iconSizePersent.height = 0;
}
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
- (void)setTitleEdgeTop:(CGFloat)titleEdgeTop {
_titleEdgeTop = titleEdgeTop;
UIEdgeInsets newEdge = self.titleEdgeInsets;
newEdge.top = titleEdgeTop;
self.titleEdgeInsets = newEdge;
}
- (void)setTitleEdgeLeft:(CGFloat)titleEdgeLeft {
_titleEdgeLeft = titleEdgeLeft;
UIEdgeInsets newEdge = self.titleEdgeInsets;
newEdge.left = titleEdgeLeft;
self.titleEdgeInsets = newEdge;
}
- (void)setTitleEdgeBottom:(CGFloat)titleEdgeBottom {
_titleEdgeBottom = titleEdgeBottom;
UIEdgeInsets newEdge = self.titleEdgeInsets;
newEdge.bottom = titleEdgeBottom;
self.titleEdgeInsets = newEdge;
}
- (void)setTitleEdgeRight:(CGFloat)titleEdgeRight {
_titleEdgeRight = titleEdgeRight;
UIEdgeInsets newEdge = self.titleEdgeInsets;
newEdge.right = titleEdgeRight;
self.titleEdgeInsets = newEdge;
}
- (CGFloat)cornerRadius {
return self.layer.cornerRadius;
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
}
- (CGFloat)borderWidth {
return self.layer.borderWidth;
}
- (void)setBorderWidth:(CGFloat)borderWidth {
self.layer.borderWidth = borderWidth;
}
- (UIColor *)borderColor {
return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setBorderColor:(UIColor *)borderColor {
self.layer.borderColor = [borderColor CGColor];
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
contentRect = [super imageRectForContentRect:contentRect];
CGSize imageSize = contentRect.size;
if(_iconSizePersent.width == 0){
imageSize.width = 0;
}else{
imageSize.width = imageSize.width * _iconSizePersent.width;
}
if(_iconSizePersent.height == 0){
imageSize.height = 0;
}else{
imageSize.height = imageSize.height * _iconSizePersent.height;
}
CGRect tmp =contentRect;
tmp.size = imageSize;
CGPoint tmpPoint = contentRect.origin;
switch (_iconContentMode) {
case IconContentModeCenter:
tmpPoint.x = tmpPoint.x + (contentRect.size.width - imageSize.width) / 2;
tmpPoint.y = tmpPoint.y + (contentRect.size.height - imageSize.height) / 2;
break;
case IconContentModeLeft:
tmpPoint.y = tmpPoint.y + (contentRect.size.height - imageSize.height) / 2;
break;
case IconContentModeRight:
tmpPoint.x = tmpPoint.x + (contentRect.size.width - imageSize.width);
tmpPoint.y = tmpPoint.y + (contentRect.size.height - imageSize.height) / 2;
break;
}
tmp.origin = tmpPoint;
return tmp;
}
@end