-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAPSButton.m
287 lines (219 loc) · 6.43 KB
/
CAPSButton.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// CAPSButton.m
// Designables
//
// Created by Niklas Fahl on 7/14/15.
// Copyright (c) 2015 CAPS. All rights reserved.
//
#import "CAPSButton.h"
#pragma mark - UIColor - ColorModifications
@interface UIColor (ColorModifications)
+ (BOOL)isColorLight:(UIColor *)color;
+ (UIColor *)getColorForBrightness:(BOOL)isLight withColor:(UIColor *)color;
@end
@implementation UIColor (ColorModifications)
+ (BOOL)isColorLight:(UIColor *)color
{
const CGFloat *componentColors = CGColorGetComponents(color.CGColor);
CGFloat colorBrightness = ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
if (colorBrightness < 0.35)
{
return NO;
}
else
{
return YES;
}
}
+ (UIColor *)getColorForBrightness:(BOOL)isLight withColor:(UIColor *)color
{
const CGFloat *componentColors = CGColorGetComponents(color.CGColor);
UIColor *newColor;
if (isLight) {
newColor = [UIColor colorWithRed:componentColors[0] > 0.2 ? componentColors[0] - 0.2 : 0.0 green:componentColors[1] > 0.2 ? componentColors[1] - 0.2 : 0.0 blue:componentColors[2] > 0.2 ? componentColors[2] - 0.2 : 0.0 alpha:componentColors[3]];
} else {
newColor = [UIColor colorWithRed:componentColors[0] < 0.8 ? componentColors[0] + 0.2 : 1.0 green:componentColors[1] < 0.8 ? componentColors[1] + 0.2 : 1.0 blue:componentColors[2] < 0.8 ? componentColors[2] + 0.2 : 1.0 alpha:componentColors[3]];
}
return newColor;
}
@end
#pragma mark - CAPS Button
@interface CAPSButton ()
{
UIColor *_highlightedColor;
UIColor *_defaultTextColor;
UIColor *_disabledTextColor;
UIColor *_fillColor;
UIColor *_borderColor;
UIColor *_borderTextColor;
CGFloat _cornerRadius;
CGFloat _borderWidth;
BOOL _hasBorderFill;
BOOL _isPill;
BOOL _isEnabled;
BOOL _isLightColor;
BOOL _isTapping;
}
@end
@implementation CAPSButton
#pragma mark - Initializers
- (id)initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
if (self) {
// set defaults
[self setPropertyDefaults];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// set defaults
[self setPropertyDefaults];
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
// set defaults
[self setPropertyDefaults];
}
return self;
}
#pragma mark - Default Property Values
- (void)setPropertyDefaults
{
_fillColor = [UIColor whiteColor];
_borderColor = [UIColor blackColor];
_borderTextColor = _fillColor;
_cornerRadius = 0;
_borderWidth = 0;
_hasBorderFill = NO;
_isPill = NO;
_isEnabled = YES;
_isLightColor = YES;
_highlightedColor = [UIColor getColorForBrightness:_isLightColor withColor:_fillColor];
_isTapping = NO;
}
#pragma mark - Update Button UI
- (void)updateButtonUserInterface
{
self.layer.masksToBounds = YES;
if (!_isTapping) {
self.layer.borderWidth = _borderWidth;
if (_isPill)
{
self.layer.cornerRadius = MIN(self.bounds.size.width, self.bounds.size.height) / 2.0;
}
else
{
self.layer.cornerRadius = _cornerRadius;
}
if (_isEnabled)
{
self.backgroundColor = _fillColor;
[self setTitleColor:_defaultTextColor forState:UIControlStateNormal];
self.layer.borderColor = _borderColor.CGColor;
}
else
{
self.backgroundColor = _highlightedColor;
[self setTitleColor:_disabledTextColor forState:UIControlStateNormal];
self.layer.borderColor = [UIColor getColorForBrightness:[UIColor isColorLight:_borderColor] withColor:_borderColor].CGColor;
}
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self updateButtonUserInterface];
}
#pragma mark - Setters
- (void)setIsEnabled:(BOOL)isEnabled
{
_isEnabled = isEnabled;
[self updateButtonUserInterface];
}
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
[self updateButtonUserInterface];
}
- (void)setBorderWidth:(CGFloat)borderWidth
{
_borderWidth = borderWidth;
[self updateButtonUserInterface];
}
- (void)setBorderColor:(UIColor *)borderColor
{
_borderColor = borderColor;
if (_hasBorderFill) {
_highlightedColor = _borderColor;
}
[self updateButtonUserInterface];
}
- (void)setHasBorderFill:(BOOL)hasBorderFill
{
_hasBorderFill = hasBorderFill;
if (hasBorderFill) {
_highlightedColor = _borderColor;
}
[self updateButtonUserInterface];
}
- (void)setIsPill:(BOOL)isPill
{
_isPill = isPill;
[self updateButtonUserInterface];
}
- (void)setFillColor:(UIColor *)fillColor
{
_fillColor = fillColor;
_highlightedColor = [UIColor getColorForBrightness:[UIColor isColorLight:fillColor] withColor:fillColor];
_borderTextColor = _fillColor;
[self updateButtonUserInterface];
}
- (void)setTextColor:(UIColor *)textColor
{
_defaultTextColor = textColor;
_disabledTextColor = [UIColor getColorForBrightness:[UIColor isColorLight:textColor] withColor:textColor];
[self updateButtonUserInterface];
}
#pragma mark - Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_isEnabled) {
_isTapping = YES;
self.backgroundColor = _highlightedColor;
if (_hasBorderFill) {
[self setTitleColor:_borderTextColor forState:UIControlStateNormal];
}
[super touchesBegan:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_isEnabled) {
_isTapping = NO;
self.backgroundColor = _fillColor;
if (_hasBorderFill) {
[self setTitleColor:_defaultTextColor forState:UIControlStateNormal];
}
[super touchesEnded:touches withEvent:event];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_isEnabled) {
_isTapping = NO;
self.backgroundColor = _fillColor;
if (_hasBorderFill) {
[self setTitleColor:_defaultTextColor forState:UIControlStateNormal];
}
[super touchesCancelled:touches withEvent:event];
}
}
@end