Skip to content
Navigation Menu
{{ message }}
forked from jessesquires/BButton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBButton.m
More file actions
539 lines (423 loc) · 16.9 KB
/
Copy pathBButton.m
File metadata and controls
539 lines (423 loc) · 16.9 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//
// BButton.m
//
// Created by Mathieu Bolard on 31/07/12.
// Copyright (c) 2012 Mathieu Bolard. All rights reserved.
//
// https://github.com/mattlawer/BButton
//
//
// BButton is licensed under the MIT license
// http://opensource.org/licenses/MIT
//
//
// -----------------------------------------
// Edited and refactored by Jesse Squires on 2 April, 2013.
//
// http://github.com/jessesquires/BButton
//
// http://hexedbits.com
//
#import "BButton.h"
#import <CoreGraphics/CoreGraphics.h>
static CGFloat const kBButtonCornerRadiusV2 = 6.0f;
static CGFloat const kBButtonCornerRadiusV3 = 4.0f;
static NSArray * kFontAwesomeStrings;
@interface BButton ()
@property (assign, nonatomic) BButtonStyle buttonStyle;
- (void)setup;
- (void)setTextAttributesForStyle:(BButtonStyle)aStyle;
- (void)didRecieveMemoryWarningNotification:(NSNotification *)notification;
- (NSString *)stringFromFontAwesomeIcon:(FAIcon)icon;
+ (UIColor *)colorForButtonType:(BButtonType)type style:(BButtonStyle)style;
+ (UIColor *)colorForV2StyleButtonWithType:(BButtonType)type;
+ (UIColor *)colorForV3StyleButtonWithType:(BButtonType)type;
+ (NSNumber *)cornerRadiusForStyle:(BButtonStyle)aStyle;
- (NSString *)stringByTrimingWhiteSpaceFromString:(NSString *)str;
- (BOOL)isStringEmpty:(NSString *)str;
- (void)drawBButtonStyleV2InRect:(CGRect)rect withContext:(CGContextRef *)context;
- (void)drawBButtonStyleV3InRect:(CGRect)rect withContext:(CGContextRef *)context;
@end
@implementation BButton
#pragma mark - Setup
- (void)setup
{
[self setBackgroundColor:[UIColor clearColor]];
_shouldShowDisabled = YES;
_buttonStyle = BButtonStyleBootstrapV3;
[self setType:BButtonTypeDefault];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRecieveMemoryWarningNotification:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
}
- (void)setTextAttributesForStyle:(BButtonStyle)aStyle
{
switch (aStyle) {
case BButtonStyleBootstrapV2:
[[self titleLabel] setShadowOffset:CGSizeMake(0.0f, -1.0f)];
[[self titleLabel] setFont:[UIFont boldSystemFontOfSize:17.0f]];
break;
case BButtonStyleBootstrapV3:
[[self titleLabel] setShadowOffset:CGSizeMake(0.0f, 0.0f)];
[[self titleLabel] setFont:[UIFont systemFontOfSize:17.0f]];
break;
}
}
#pragma mark - Initialization
- (id)initWithFrame:(CGRect)frame type:(BButtonType)type style:(BButtonStyle)style
{
return [self initWithFrame:frame
color:[BButton colorForButtonType:type style:style]
style:style];
}
- (id)initWithFrame:(CGRect)frame
type:(BButtonType)type
style:(BButtonStyle)style
icon:(FAIcon)icon
fontSize:(CGFloat)fontSize
{
return [self initWithFrame:frame
color:[BButton colorForButtonType:type style:style]
style:style
icon:icon
fontSize:fontSize];
}
- (id)initWithFrame:(CGRect)frame color:(UIColor *)color style:(BButtonStyle)style
{
self = [self initWithFrame:frame];
if(self) {
_buttonStyle = style;
[self setColor:color];
[self setTextAttributesForStyle:style];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
color:(UIColor *)color
style:(BButtonStyle)style
icon:(FAIcon)icon
fontSize:(CGFloat)fontSize
{
self = [self initWithFrame:frame color:color style:style];
if(self) {
[[self titleLabel] setFont:[UIFont fontWithName:kFontAwesomeFont size:fontSize]];
[[self titleLabel] setTextAlignment:NSTextAlignmentCenter];
[self setTitle:[self stringFromFontAwesomeIcon:icon]
forState:UIControlStateNormal];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self) {
[self setup];
[self setTextAttributesForStyle:_buttonStyle];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
[self setup];
}
return self;
}
- (void)dealloc
{
_color = nil;
_buttonCornerRadius = nil;
kFontAwesomeStrings = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
}
#pragma mark - Class initialization
+ (BButton *)awesomeButtonWithOnlyIcon:(FAIcon)icon
type:(BButtonType)type
style:(BButtonStyle)style
{
return [BButton awesomeButtonWithOnlyIcon:icon
color:[BButton colorForButtonType:type style:style]
style:style];
}
+ (BButton *)awesomeButtonWithOnlyIcon:(FAIcon)icon
color:(UIColor *)color
style:(BButtonStyle)style
{
return [[BButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)
color:color
style:style
icon:icon
fontSize:20.0f];
}
#pragma mark - Parent overrides
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
[self setNeedsDisplay];
}
#pragma mark - UIAppearance getters
- (NSNumber *)buttonCornerRadius
{
if(!_buttonCornerRadius) {
_buttonCornerRadius = [[[self class] appearance] buttonCornerRadius];
}
if(_buttonCornerRadius) {
return _buttonCornerRadius;
}
return [BButton cornerRadiusForStyle:_buttonStyle];
}
#pragma mark - Setters
- (void)setColor:(UIColor *)newColor
{
_color = newColor;
if([newColor bb_isLightColor]) {
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleShadowColor:[[UIColor whiteColor] colorWithAlphaComponent:0.6f] forState:UIControlStateNormal];
if(self.shouldShowDisabled)
[self setTitleColor:[UIColor colorWithWhite:0.4f alpha:0.5f] forState:UIControlStateDisabled];
}
else {
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleShadowColor:[[UIColor blackColor] colorWithAlphaComponent:0.6f] forState:UIControlStateNormal];
if(self.shouldShowDisabled)
[self setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.5f] forState:UIControlStateDisabled];
}
[self setNeedsDisplay];
}
- (void)setShouldShowDisabled:(BOOL)show
{
_shouldShowDisabled = show;
if(show) {
if([self.color bb_isLightColor])
[self setTitleColor:[UIColor colorWithWhite:0.4f alpha:0.5f] forState:UIControlStateDisabled];
else
[self setTitleColor:[UIColor colorWithWhite:1.0f alpha:0.5f] forState:UIControlStateDisabled];
}
else {
if([self.color bb_isLightColor])
[self setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
else
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
}
}
#pragma mark - Notifications
- (void)didRecieveMemoryWarningNotification:(NSNotification *)notification
{
NSLog(@"%@ recieved %@", [BButton class], notification.name);
kFontAwesomeStrings = nil;
}
#pragma mark - BButton
- (void)setStyle:(BButtonStyle)style
{
_buttonStyle = style;
[self setColor:_color];
}
- (void)setType:(BButtonType)type
{
[self setColor:[BButton colorForButtonType:type
style:_buttonStyle]];
}
- (void)addAwesomeIcon:(FAIcon)icon beforeTitle:(BOOL)before
{
NSString *iconString = [self stringFromFontAwesomeIcon:icon];
self.titleLabel.font = [UIFont fontWithName:kFontAwesomeFont
size:self.titleLabel.font.pointSize];
NSString *title = [NSString stringWithFormat:@"%@", iconString];
if(self.titleLabel.text && ![self isStringEmpty:self.titleLabel.text]) {
if(before) {
title = [title stringByAppendingFormat:@" %@", self.titleLabel.text];
}
else {
title = [NSString stringWithFormat:@"%@ %@", self.titleLabel.text, iconString];
}
}
[self setTitle:title forState:UIControlStateNormal];
}
- (NSString *)stringFromFontAwesomeIcon:(FAIcon)icon
{
if(!kFontAwesomeStrings) {
kFontAwesomeStrings = [NSString fa_allFontAwesomeStrings];
}
return [NSString fa_stringFromFontAwesomeStrings:kFontAwesomeStrings
forIcon:icon];
}
+ (UIColor *)colorForButtonType:(BButtonType)type style:(BButtonStyle)style
{
switch (style) {
case BButtonStyleBootstrapV2:
return [BButton colorForV2StyleButtonWithType:type];
case BButtonStyleBootstrapV3:
default:
return [BButton colorForV3StyleButtonWithType:type];
}
}
+ (UIColor *)colorForV2StyleButtonWithType:(BButtonType)type
{
switch (type) {
case BButtonTypePrimary:
return [UIColor bb_primaryColorV2];
case BButtonTypeInfo:
return [UIColor bb_infoColorV2];
case BButtonTypeSuccess:
return [UIColor bb_successColorV2];
case BButtonTypeWarning:
return [UIColor bb_warningColorV2];
case BButtonTypeDanger:
return [UIColor bb_dangerColorV2];
case BButtonTypeInverse:
return [UIColor bb_inverseColorV2];
case BButtonTypeTwitter:
return [UIColor bb_twitterColor];
case BButtonTypeFacebook:
return [UIColor bb_facebookColor];
case BButtonTypePurple:
return [UIColor bb_purpleBButtonColor];
case BButtonTypeGray:
return [UIColor bb_grayBButtonColor];
case BButtonTypeDefault:
default:
return [UIColor bb_defaultColorV2];
}
}
+ (UIColor *)colorForV3StyleButtonWithType:(BButtonType)type
{
switch (type) {
case BButtonTypePrimary:
return [UIColor bb_primaryColorV3];
case BButtonTypeInfo:
return [UIColor bb_infoColorV3];
case BButtonTypeSuccess:
return [UIColor bb_successColorV3];
case BButtonTypeWarning:
return [UIColor bb_warningColorV3];
case BButtonTypeDanger:
return [UIColor bb_dangerColorV3];
case BButtonTypeInverse:
return [UIColor bb_inverseColorV3];
case BButtonTypeTwitter:
return [UIColor bb_twitterColor];
case BButtonTypeFacebook:
return [UIColor bb_facebookColor];
case BButtonTypePurple:
return [UIColor bb_purpleBButtonColor];
case BButtonTypeGray:
return [UIColor bb_grayBButtonColor];
case BButtonTypeDefault:
default:
return [UIColor bb_defaultColorV3];
}
}
+ (NSNumber *)cornerRadiusForStyle:(BButtonStyle)aStyle
{
CGFloat r = 0.0f;
switch (aStyle) {
case BButtonStyleBootstrapV2:
r = kBButtonCornerRadiusV2;
break;
case BButtonStyleBootstrapV3:
r = kBButtonCornerRadiusV3;
break;
}
return [NSNumber numberWithFloat:r];
}
#pragma mark - Utilities
- (NSString *)stringByTrimingWhiteSpaceFromString:(NSString *)str
{
return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (BOOL)isStringEmpty:(NSString *)str
{
return [[self stringByTrimingWhiteSpaceFromString:str] isEqualToString:@""];
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
if(!context)
return;
switch (self.buttonStyle) {
case BButtonStyleBootstrapV2:
[self drawBButtonStyleV2InRect:rect withContext:&context];
break;
case BButtonStyleBootstrapV3:
[self drawBButtonStyleV3InRect:rect withContext:&context];
break;
}
}
- (void)drawBButtonStyleV2InRect:(CGRect)rect withContext:(CGContextRef *)context
{
UIColor *border = [self.color bb_darkenColorWithValue:0.06f];
UIColor *shadow = [self.color bb_lightenColorWithValue:0.50f];
CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
CGFloat shadowBlurRadius = 2.0f;
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5f, 0.5f, rect.size.width-1.0f, rect.size.height-1.0f)
cornerRadius:[self.buttonCornerRadius floatValue]];
CGContextSaveGState(*context);
[roundedRectanglePath addClip];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *topColor = (self.shouldShowDisabled && !self.enabled) ? [self.color bb_darkenColorWithValue:0.12f] : [self.color bb_lightenColorWithValue:0.12f];
NSArray *newGradientColors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)self.color.CGColor, nil];
CGFloat newGradientLocations[] = {0.0f, 1.0f};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)newGradientColors, newGradientLocations);
CGColorSpaceRelease(colorSpace);
CGContextDrawLinearGradient(*context,
gradient,
CGPointMake(0.0f, self.highlighted ? rect.size.height - 0.5f : 0.5f),
CGPointMake(0.0f, self.highlighted ? 0.5f : rect.size.height - 0.5f), 0.0f);
CGGradientRelease(gradient);
CGContextRestoreGState(*context);
if(!self.highlighted) {
// Rounded Rectangle Inner Shadow
CGRect roundedRectangleBorderRect = CGRectInset([roundedRectanglePath bounds], -shadowBlurRadius, -shadowBlurRadius);
roundedRectangleBorderRect = CGRectOffset(roundedRectangleBorderRect, -shadowOffset.width, -shadowOffset.height);
roundedRectangleBorderRect = CGRectInset(CGRectUnion(roundedRectangleBorderRect, [roundedRectanglePath bounds]), -1.0f, -1.0f);
UIBezierPath *roundedRectangleNegativePath = [UIBezierPath bezierPathWithRect: roundedRectangleBorderRect];
[roundedRectangleNegativePath appendPath: roundedRectanglePath];
roundedRectangleNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(*context);
CGFloat xOffset = shadowOffset.width + round(roundedRectangleBorderRect.size.width);
CGFloat yOffset = shadowOffset.height;
CGContextSetShadowWithColor(*context,
CGSizeMake(xOffset + copysign(0.1f, xOffset), yOffset + copysign(0.1f, yOffset)),
shadowBlurRadius,
shadow.CGColor);
[roundedRectanglePath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(roundedRectangleBorderRect.size.width), 0.0f);
[roundedRectangleNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[roundedRectangleNegativePath fill];
CGContextRestoreGState(*context);
}
[border setStroke];
roundedRectanglePath.lineWidth = 1.0f;
[roundedRectanglePath stroke];
}
- (void)drawBButtonStyleV3InRect:(CGRect)rect withContext:(CGContextRef *)context
{
CGContextSaveGState(*context);
UIColor *fill = (!self.highlighted) ? self.color : [self.color bb_darkenColorWithValue:0.06f];
if(!self.enabled)
[fill bb_desaturatedColorToPercentSaturation:0.60f];
CGContextSetFillColorWithColor(*context, fill.CGColor);
UIColor *border = (!self.highlighted) ? [self.color bb_darkenColorWithValue:0.06f] : [self.color bb_darkenColorWithValue:0.12f];
if(!self.enabled)
[border bb_desaturatedColorToPercentSaturation:0.60f];
CGContextSetStrokeColorWithColor(*context, border.CGColor);
CGContextSetLineWidth(*context, 1.0f);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5f, 0.5f, rect.size.width-1.0f, rect.size.height-1.0f)
cornerRadius:[self.buttonCornerRadius floatValue]];
CGContextAddPath(*context, path.CGPath);
CGContextDrawPath(*context, kCGPathFillStroke);
CGContextRestoreGState(*context);
}
@end
You can’t perform that action at this time.
