forked from dogo/SCLAlertView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCLButton.m
More file actions
executable file
·167 lines (142 loc) · 4.43 KB
/
SCLButton.m
File metadata and controls
executable file
·167 lines (142 loc) · 4.43 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
//
// SCLButton.m
// SCLAlertView
//
// Created by Diogo Autilio on 9/26/14.
// Copyright (c) 2014-2016 AnyKey Entertainment. All rights reserved.
//
#import "SCLButton.h"
#import "SCLTimerDisplay.h"
#define MARGIN_BUTTON 12.0f
#define DEFAULT_WINDOW_WIDTH 240
#define MIN_HEIGHT 35.0f
@implementation SCLButton
- (instancetype)init
{
self = [super init];
if (self)
{
[self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
}
return self;
}
- (instancetype)initWithWindowWidth:(CGFloat)windowWidth
{
self = [super init];
if (self)
{
[self setupWithWindowWidth:windowWidth];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setupWithWindowWidth:DEFAULT_WINDOW_WIDTH];
}
return self;
}
- (void)setupWithWindowWidth:(CGFloat)windowWidth
{
self.frame = CGRectMake(0.0f, 0.0f, windowWidth - (MARGIN_BUTTON * 2), MIN_HEIGHT);
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.layer.cornerRadius = 3.0f;
}
- (void)adjustWidthWithWindowWidth:(CGFloat)windowWidht numberOfButtons:(NSUInteger)numberOfButtons
{
CGFloat allButtonsWidth = windowWidht - (MARGIN_BUTTON * 2);
CGFloat buttonWidth = (allButtonsWidth - ((numberOfButtons - 1) * 10)) / numberOfButtons;
self.frame = CGRectMake(0.0f, 0.0f, buttonWidth, MIN_HEIGHT);
}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
self.titleLabel.numberOfLines = 0;
// Update title frame.
[self.titleLabel sizeToFit];
// Update button frame
[self layoutIfNeeded];
// Get height needed to display title label completely
CGFloat buttonHeight = MAX(self.titleLabel.frame.size.height, MIN_HEIGHT);
// Update button frame
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, buttonHeight);
}
- (void)setHighlighted:(BOOL)highlighted
{
self.backgroundColor = (highlighted) ? [self darkerColorForColor:_defaultBackgroundColor] : _defaultBackgroundColor;
[super setHighlighted:highlighted];
}
- (void)setDefaultBackgroundColor:(UIColor *)defaultBackgroundColor
{
self.backgroundColor = _defaultBackgroundColor = defaultBackgroundColor;
}
- (void)setTimer:(SCLTimerDisplay *)timer
{
_timer = timer;
[self addSubview:timer];
[timer updateFrame:self.frame.size];
timer.color = self.titleLabel.textColor;
}
#pragma mark - Button Apperance
- (void)parseConfig:(NSDictionary *)buttonConfig
{
if (buttonConfig[@"backgroundColor"])
{
self.defaultBackgroundColor = buttonConfig[@"backgroundColor"];
}
if (buttonConfig[@"textColor"])
{
[self setTitleColor:buttonConfig[@"textColor"] forState:UIControlStateNormal];
}
if (buttonConfig[@"cornerRadius"])
{
self.layer.cornerRadius = [buttonConfig[@"cornerRadius"] floatValue];
}
if ((buttonConfig[@"borderColor"]) && (buttonConfig[@"borderWidth"]))
{
self.layer.borderColor = ((UIColor*)buttonConfig[@"borderColor"]).CGColor;
self.layer.borderWidth = [buttonConfig[@"borderWidth"] floatValue];
}
else if (buttonConfig[@"borderWidth"])
{
self.layer.borderWidth = [buttonConfig[@"borderWidth"] floatValue];
}
// Add Button custom font with buttonConfig parameters
if (buttonConfig[@"font"]) {
self.titleLabel.font = buttonConfig[@"font"];
}
}
#pragma mark - Helpers
- (UIColor *)darkerColorForColor:(UIColor *)color
{
CGFloat r, g, b, a;
if ([color getRed:&r green:&g blue:&b alpha:&a])
return [UIColor colorWithRed:MAX(r - 0.2f, 0.0f)
green:MAX(g - 0.2f, 0.0f)
blue:MAX(b - 0.2f, 0.0f)
alpha:a];
return nil;
}
- (UIColor *)lighterColorForColor:(UIColor *)color
{
CGFloat r, g, b, a;
if ([color getRed:&r green:&g blue:&b alpha:&a])
return [UIColor colorWithRed:MIN(r + 0.2f, 1.0f)
green:MIN(g + 0.2f, 1.0f)
blue:MIN(b + 0.2f, 1.0f)
alpha:a];
return nil;
}
@end