-
Notifications
You must be signed in to change notification settings - Fork 29
/
ACPButton.m
executable file
·406 lines (272 loc) · 12.7 KB
/
ACPButton.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// ACPButton.m
//
// Created by Antonio Casero
// Copyright (c) 2013 Antonio Casero. All rights reserved.
//
#import "ACPButton.h"
//Values by default - Set up this values depending of your project design!
#define kLabelFont [UIFont fontWithName:@"HelveticaNeue" size:15]
#define kColorFontNormal [UIColor whiteColor]
#define kColorFontHighlighted [UIColor whiteColor]
#define kColorFontDisable [UIColor grayColor]
#define kColorShadowNormal [UIColor blackColor]
#define kColorShadowHighlighted [UIColor whiteColor]
#define kColorShadowDisable [UIColor clearColor]
#define kShadowOffset CGSizeMake(0, 0)
#define kCornerRadius 5
@interface ACPButton ()
@property (strong, nonatomic) UIColor * borderColor;
@property (strong, nonatomic) UIColor * inneColor;
@property (strong, nonatomic) UIColor * topColor;
@property (strong, nonatomic) UIColor * bottomColor;
//Flat style
@property (assign, nonatomic) BOOL isFlat;
@property (strong, nonatomic) UIColor * normalStateColor;
@property (strong, nonatomic) UIColor * highlightedColor;
@property (assign, nonatomic) NSInteger buttonRadius;
//Glow
@property (assign, nonatomic) BOOL glowActivated;
@end
@implementation ACPButton
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
/*--------------------------------------------------------------------------
This type of button is created by default.
--------------------------------------------------------------------------*/
[self setStyleType:ACPButtonGrey];
_glowActivated = NO; //By default
}
return self;
}
- (void) setStyleType:(ACPButtonType)style {
_isFlat = NO;
switch (style) {
/*--------------------------------------------------------------------------
You can set this values acording with your project
--------------------------------------------------------------------------*/
case ACPButtonOK:
_topColor = [UIColor colorWithRed:0.588235 green:0.847059 blue:0.396078 alpha:1];
_bottomColor=[UIColor colorWithRed:0.388235 green:0.650980 blue:0.184314 alpha:1];
_borderColor = [UIColor colorWithRed:0.388235 green:0.650980 blue:0.184314 alpha:1] ;
_inneColor = nil;
break;
case ACPButtonBlue:
_topColor = [UIColor colorWithRed:0.423529 green:0.713726 blue:0.921569 alpha:1];
_bottomColor=[UIColor colorWithRed:0.211765 green:0.482353 blue:0.815686 alpha:1];
_borderColor = [UIColor colorWithRed:0.250980 green:0.505882 blue:0.686275 alpha:1] ;
_inneColor = nil;
break;
case ACPButtonGrey:
_topColor = [UIColor colorWithRed:0.627451 green:0.627451 blue:0.627451 alpha:1];
_bottomColor=[UIColor colorWithRed:0.509804 green:0.509804 blue:0.509804 alpha:1];
_borderColor = [UIColor colorWithRed:0.627451 green:0.627451 blue:0.627451 alpha:1] ;
_inneColor = nil;
break;
case ACPButtonCancel:
_topColor = [UIColor colorWithRed:0.929412 green:0.392157 blue:0.290196 alpha:1];
_bottomColor=[UIColor colorWithRed:0.839216 green:0.282353 blue:0.270588 alpha:1];
_borderColor = [UIColor colorWithRed:0.815686 green:0.215686 blue:0.145098 alpha:1];
_inneColor = nil;
break;
case ACPButtonDarkGrey:
_topColor = [UIColor colorWithRed:0.525490 green:0.525490 blue:0.525490 alpha:1];
_bottomColor=[UIColor colorWithRed:0.262745 green:0.258824 blue:0.262745 alpha:1];
_borderColor = [UIColor colorWithRed:0.117647 green:0.117647 blue:0.117647 alpha:1];
_inneColor = nil;
break;
case ACPButtonPurple:
_topColor = [UIColor colorWithRed:0.807843 green:0.552941 blue:0.929412 alpha:1];
_bottomColor=[UIColor colorWithRed:0.603922 green:0.368627 blue:0.784314 alpha:1];
_borderColor = [UIColor colorWithRed:0.364706 green:0.168627 blue:0.541176 alpha:1];
_inneColor = nil;
break;
default:
break;
}
self.buttonRadius = 5;
[self setTextStyle];
}
- (void) setFlatStyleType:(ACPButtonType)style {
[self setStyleType:style];
_isFlat = YES;
self.buttonRadius = 0;
[self setTextStyle];
}
- (void) setFlatStyle:(UIColor*)normal andHighlightedColor:(UIColor*)highlighted {
[self setStyle:normal andBottomColor:highlighted];
_isFlat = YES;
self.buttonRadius = 0;
[self setTextStyle];
}
- (void) setStyle:(UIColor*)topColor andBottomColor:(UIColor*)bottomColor {
_isFlat = NO;
_topColor = topColor;
_bottomColor = bottomColor;
if(!_bottomColor)
_bottomColor = topColor;
[self setNeedsDisplay];
[self setTextStyle];
}
- (void) setBorderStyle:(UIColor*)borderColor andInnerColor:(UIColor*)innerColor {
_borderColor = borderColor;
_inneColor = innerColor;
[self setNeedsDisplay];
}
- (void) setCornerRadius:(NSInteger)value {
self.buttonRadius = value;
}
- (void) setGlowHighlightedState:(BOOL)glowOption {
self.glowActivated = glowOption;
}
- (void) turnOnGlow {
self.titleLabel.layer.shadowColor = [UIColor whiteColor].CGColor;
self.titleLabel.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.titleLabel.layer.shadowRadius = 10.0;
self.titleLabel.layer.shadowOpacity = 0.8;
self.titleLabel.layer.masksToBounds = NO;
}
- (void) turnOffGlow {
self.titleLabel.layer.shadowColor = nil;
self.titleLabel.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.titleLabel.layer.shadowRadius = 0;
self.titleLabel.layer.shadowOpacity = 0;
self.titleLabel.layer.masksToBounds = NO;
}
//----------------------------------------------------------------------------------------------------------------
# pragma mark -
# pragma mark Override the uibutton methods
# pragma mark -
//----------------------------------------------------------------------------------------------------------------
- (void)setHighlighted:(BOOL)highlighted
{
[self setNeedsDisplay];
if(_glowActivated){
if(highlighted){
[self turnOnGlow];
}
else {
[self turnOffGlow];
}
}
[super setHighlighted:highlighted];
}
# pragma mark -
# pragma mark Set up the button with a image
# pragma mark -
- (void)setStyleWithImage:(NSString*)image highlightedImage:(NSString*)hImage disableImage:(NSString*)dImage andInsets:(UIEdgeInsets)insets {
// Define resizable images
UIImage *resizableButton = [[UIImage imageNamed:image] resizableImageWithCapInsets:insets];
UIImage *resizableButtonHighlighted = [[UIImage imageNamed:hImage] resizableImageWithCapInsets:insets];
UIImage *resizableButtonDisabled = [[UIImage imageNamed:dImage] resizableImageWithCapInsets:insets];
// Set resizable background image
[self setBackgroundImage:resizableButton forState:UIControlStateNormal];
[self setBackgroundImage:resizableButtonHighlighted forState:UIControlStateHighlighted];
[self setBackgroundImage:resizableButtonDisabled forState:UIControlStateDisabled];
[self setTextStyle];
_topColor=nil;
_borderColor=nil;
_inneColor = nil;
_bottomColor = nil;
}
# pragma mark -
# pragma mark Set up the button text
# pragma mark -
- (void) setTextStyle {
/*--------------------------------------------------------------------------
By default
--------------------------------------------------------------------------*/
[self.titleLabel setFont:kLabelFont];
[self.titleLabel setShadowOffset:kShadowOffset];
[self setTitleShadowColor:kColorShadowNormal forState:UIControlStateNormal];
[self setTitleColor:kColorFontNormal forState:UIControlStateNormal];
[self setTitleShadowColor:kColorShadowHighlighted forState:UIControlStateHighlighted];
[self setTitleColor:kColorFontHighlighted forState:UIControlStateHighlighted];
[self setTitleShadowColor:kColorShadowDisable forState:UIControlStateDisabled];
[self setTitleColor:kColorFontDisable forState:UIControlStateDisabled];
}
/*--------------------------------------------------------------------------
Define the color of the label for every state
--------------------------------------------------------------------------*/
- (void) setLabelTextColor:(UIColor*)nColor highlightedColor:(UIColor*)hColor disableColor:(UIColor*)dColor{
[self setTitleColor:nColor forState:UIControlStateNormal];
[self setTitleColor:hColor forState:UIControlStateHighlighted];
[self setTitleColor:dColor forState:UIControlStateDisabled];
}
/*--------------------------------------------------------------------------
Define the color of the text shadow
--------------------------------------------------------------------------*/
- (void) setLabelTextShadow:(CGSize)shadowOffSet normalColor:(UIColor*)nColor highlightedColor:(UIColor*)hColor disableColor:(UIColor*)dColor{
[self.titleLabel setShadowOffset:shadowOffSet];
[self setTitleShadowColor:nColor forState:UIControlStateNormal];
[self setTitleShadowColor:hColor forState:UIControlStateHighlighted];
[self setTitleShadowColor:dColor forState:UIControlStateDisabled];
}
- (void) setLabelFont:(UIFont*)font {
/*--------------------------------------------------------------------------
If you want to change the font by default, change the macro on top of
this class.
--------------------------------------------------------------------------*/
[self.titleLabel setFont:font];
}
# pragma mark -
# pragma mark Draw rect overrides
# pragma mark -
- (void)drawRect:(CGRect)rect
{
// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) cornerRadius: self.buttonRadius];
// Use the bezier as a clipping path
[roundedRectanglePath addClip];
if(!_isFlat){
if (_topColor){
// Color Declarations
UIColor *topColor = _topColor;
UIColor *bottomColor =_bottomColor;
// Gradient Declarations
NSArray *gradientColors = (@[
(id)_topColor.CGColor,
(id)_bottomColor.CGColor
]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(gradientColors), NULL);
NSArray *highlightedGradientColors = (@[
(id)bottomColor.CGColor,
(id)topColor.CGColor
]);
CGGradientRef highlightedGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)(highlightedGradientColors), NULL);
// Draw rounded rectangle bezier path
// Use one of the two gradients depending on the state of the button
CGGradientRef background = self.highlighted? highlightedGradient : gradient;
// Draw gradient within the path
CGContextDrawLinearGradient(context, background, CGPointMake(140, 0), CGPointMake(140, self.frame.size.height), 0);
//Release the gradient
CGGradientRelease(gradient);
CGGradientRelease(highlightedGradient);
}
} else
{
//Solid colors
CGColorRef colorRef = self.highlighted? _topColor.CGColor : _bottomColor.CGColor;
CGContextSetFillColor(context, CGColorGetComponents(colorRef));
CGContextFillRect(context, rect);
}
// Draw border
if(_borderColor){
[_borderColor setStroke];
roundedRectanglePath.lineWidth = 2;
[roundedRectanglePath stroke];
}
// Draw Inner Glow
if(_inneColor){
UIBezierPath *innerGlowRect = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(1.5, 1.5, self.frame.size.width-2, self.frame.size.height-2) cornerRadius: 0];
[_inneColor setStroke];
innerGlowRect.lineWidth = 1;
[innerGlowRect stroke];
}
// Cleanup
CGColorSpaceRelease(colorSpace);
}
@end