-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNiftyButton.m
executable file
·253 lines (198 loc) · 9.69 KB
/
NiftyButton.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
/*
The MIT License (MIT)
Copyright (c) 2012 Stephen Melvin <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "NiftyButton.h"
#import <UIKit/UIKit.h>
#define BORDER_RADIUS 4
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface NiftyButton ()
@property (nonatomic) NSString *theme;
@property (nonatomic) UIColor *bottomColor;
@property (nonatomic) UIColor *topColor;
@property (nonatomic) UIColor *borderColor;
@end
@implementation NiftyButton
+ (id)buttonWithBorderColor:(UIColor *)aBorderColor topColor:(UIColor *)aTopColor bottomColor:(UIColor *)aBottomColor frame:(CGRect)aFrame{
NiftyButton *newButton = [[NiftyButton alloc] initWithFrame:aFrame];
if(newButton){
[newButton configureAllControlStatesUsingBorderColor:aBorderColor topColor:aTopColor bottomColor:aBottomColor];
[newButton setLabelColorWhite];
}
return newButton;
}
+ (id)buttonWithTheme:(NiftyButtonColorTheme)colorTheme frame:(CGRect)aFrame{
NiftyButton *newButton = [[NiftyButton alloc] initWithFrame:aFrame];
if(newButton){
[newButton setColorTheme:colorTheme];
}
return newButton;
}
- (void)awakeFromNib{
[self setLabelColorBlack];
// Configure the button's colors using user-defined keys
if (self.theme) {
if([self.theme isEqualToString:@"red"]){
[self setColorTheme:NiftyButtonRedTheme];
}
else if([self.theme isEqualToString:@"white"]){
[self setColorTheme:NiftyButtonWhiteTheme];
}
else if([self.theme isEqualToString:@"blue"]){
[self setColorTheme:NiftyButtonBlueTheme];
}
else if([self.theme isEqualToString:@"black"]){
[self setColorTheme:NiftyButtonBlackTheme];
}
else if([self.theme isEqualToString:@"green"]){
[self setColorTheme:NiftyButtonGreenTheme];
}
else if([self.theme isEqualToString:@"yellow"]){
[self setColorTheme:NiftyButtonYellowTheme];
}
}
else if(self.topColor && self.bottomColor && self.borderColor){
[self configureAllControlStatesUsingBorderColor:self.borderColor topColor:self.topColor bottomColor:self.bottomColor];
}
else {
[self setColorTheme:NiftyButtonWhiteTheme];
}
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self){
self.frame = frame;
[self setColorTheme:NiftyButtonWhiteTheme];
[self setLabelColorBlack];
}
return self;
}
- (void)configureAllControlStatesUsingBorderColor:(UIColor *)aBorderColor topColor:(UIColor *)aTopColor bottomColor:(UIColor *)aBottomColor{
[self setButtonBorderColor:aBorderColor topColor:aTopColor bottomColor:aBottomColor forState:UIControlStateNormal];
[self setButtonBorderColor:aBorderColor topColor:aTopColor bottomColor:aBottomColor forState:UIControlStateDisabled];
[self setButtonBorderColor:aBorderColor topColor:aBottomColor bottomColor:aTopColor forState:UIControlStateHighlighted];
[self setButtonBorderColor:aBorderColor topColor:aBottomColor bottomColor:aTopColor forState:UIControlStateSelected];
}
- (void)configureAllControlStatesUsingTheme:(NiftyButtonColorTheme)aTheme{
[self setColorTheme:aTheme forState:UIControlStateNormal showDepressed:NO];
[self setColorTheme:aTheme forState:UIControlStateDisabled showDepressed:NO];
[self setColorTheme:aTheme forState:UIControlStateHighlighted showDepressed:YES];
[self setColorTheme:aTheme forState:UIControlStateSelected showDepressed:YES];
}
- (void)setColorTheme:(NiftyButtonColorTheme)aTheme{
[self configureAllControlStatesUsingTheme:aTheme];
}
- (void)setColorTheme:(NiftyButtonColorTheme)aTheme forState:(UIControlState)aState showDepressed:(BOOL)depressed{
UIColor *borderColor, *topColor, *bottomColor;
switch (aTheme) {
case NiftyButtonBlackTheme:
borderColor = UIColorFromRGB(0x303030);
topColor = UIColorFromRGB(0x444444);
bottomColor = UIColorFromRGB(0x222222);
break;
case NiftyButtonWhiteTheme:
borderColor = UIColorFromRGB(0xd0d0d0);
topColor = UIColorFromRGB(0xFFFFFF);
bottomColor = UIColorFromRGB(0xE6E6E6);
break;
case NiftyButtonBlueTheme:
borderColor = UIColorFromRGB(0x0062b8);
topColor = UIColorFromRGB(0x0084cc);
bottomColor = UIColorFromRGB(0x0045cc);
break;
case NiftyButtonGreenTheme:
borderColor = UIColorFromRGB(0x52a552);
topColor = UIColorFromRGB(0x62C462);
bottomColor = UIColorFromRGB(0x51A351);
break;
case NiftyButtonRedTheme:
borderColor = UIColorFromRGB(0xc44741);
topColor = UIColorFromRGB(0xEE5F5B);
bottomColor = UIColorFromRGB(0xBD362F);
break;
case NiftyButtonYellowTheme:
borderColor = UIColorFromRGB(0xe1962d);
topColor = UIColorFromRGB(0xFBB450);
bottomColor = UIColorFromRGB(0xF89406);
break;
}
if(aTheme == NiftyButtonWhiteTheme){
[self setLabelColorBlack];
}
else{
[self setLabelColorWhite];
}
if (depressed) {
[self setButtonBorderColor:borderColor topColor:bottomColor bottomColor:topColor forState:aState];
}
else {
[self setButtonBorderColor:borderColor topColor:topColor bottomColor:bottomColor forState:aState];
}
}
- (void)setLabelColorWhite{
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[self setTitleShadowColor:[UIColor colorWithWhite:0.0 alpha:0.25] forState:UIControlStateNormal];
[self setTitleShadowColor:[UIColor colorWithWhite:0.0 alpha:0.25] forState:UIControlStateHighlighted];
[self setTitleShadowColor:[UIColor colorWithWhite:0.0 alpha:0.25] forState:UIControlStateSelected];
[self setTitleShadowColor:[UIColor colorWithWhite:0.0 alpha:0.25] forState:UIControlStateDisabled];
[[self titleLabel] setShadowOffset:CGSizeMake(0, -1)];
}
- (void)setLabelColorBlack{
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
[[self titleLabel] setShadowOffset:CGSizeMake(0, 0)];
}
- (void)setButtonBorderColor:(UIColor *)aBorderColor topColor:(UIColor *)aTopColor bottomColor:(UIColor *)aBottomColor forState:(UIControlState)aState{
UIImage *backgroundImage = [self backgroundImageWithBorderColor:aBorderColor topColor:aTopColor bottomColor:aBottomColor];
[self setBackgroundImage:backgroundImage forState:aState];
}
- (UIImage *)backgroundImageWithBorderColor:(UIColor*)aBorderColor topColor:(UIColor*)aTopColor bottomColor:(UIColor*)aBottomColor{
float width = self.frame.size.width;
float height = self.frame.size.height;
// Create bitmap context and color space
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *innerGlow = [UIColor colorWithWhite:1.0 alpha:0.1];
// Gradient Declarations
NSArray *gradientColors = (@[(id)aTopColor.CGColor, (id)aBottomColor.CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(gradientColors), NULL);
// Draw rounded rectangle bezier path
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, width, height) cornerRadius: BORDER_RADIUS];
// Use the bezier as a clipping path
[roundedRectanglePath addClip];
// Draw gradient within the path
CGContextDrawLinearGradient(context, gradient, CGPointMake(140, 0), CGPointMake(140, height-1), 0);
// Draw border
[aBorderColor setStroke];
roundedRectanglePath.lineWidth = 2;
[roundedRectanglePath stroke];
// Draw Inner Glow
UIBezierPath *innerGlowRect = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(1.5, 1.5, width - 3, height - 3) cornerRadius: BORDER_RADIUS * 4/5];
[innerGlow setStroke];
innerGlowRect.lineWidth = 1;
[innerGlowRect stroke];
// Output as Image
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
// Cleanup
UIGraphicsEndImageContext();
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
return backgroundImage;
}
- (void)setTitleForAllStates:(NSString *)title{
[self setTitle:title forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateReserved];
[self setTitle:title forState:UIControlStateSelected];
[self setTitle:title forState:UIControlStateHighlighted];
[self setTitle:title forState:UIControlStateDisabled];
}
@end