-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFUISegmentedControl.m
executable file
·218 lines (188 loc) · 9.63 KB
/
FUISegmentedControl.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
//
// FUISegmentedControl.m
// FlatUIKitExample
//
// Created by Alex Medearis on 5/17/13.
//
//
#import "FUISegmentedControl.h"
#import "UIImage+FlatUI.h"
#import "UIColor+FlatUI.h"
#import "UIFont+FlatUI.h"
@implementation FUISegmentedControl
+ (void)initialize {
if (self == [FUISegmentedControl class]) {
FUISegmentedControl *appearance = [self appearance];
[appearance setCornerRadius:5.0f];
[appearance setSelectedColor:[UIColor blueColor]];
[appearance setDeselectedColor:[UIColor darkGrayColor]];
[appearance setSelectedFontColor:[UIColor whiteColor]];
[appearance setDeselectedFontColor:[UIColor whiteColor]];
}
}
- (void)setDeselectedColor:(UIColor *)deselectedColor {
_deselectedColor = deselectedColor;
[self configureFlatSegmentedControl];
}
- (void)setSelectedColor:(UIColor *)selectedColor {
_selectedColor = selectedColor;
[self configureFlatSegmentedControl];
}
- (void)setDisabledColor:(UIColor *)disabledColor {
_disabledColor = disabledColor;
[self configureFlatSegmentedControl];
}
- (void)setDividerColor:(UIColor *)dividerColor {
_dividerColor = dividerColor;
[self configureFlatSegmentedControl];
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
[self configureFlatSegmentedControl];
}
- (void)setSelectedFont:(UIFont *)selectedFont {
_selectedFont = selectedFont;
[self setupFonts];
}
- (void)setSelectedFontColor:(UIColor *)selectedFontColor {
_selectedFontColor = selectedFontColor;
[self setupFonts];
}
- (void)setDeselectedFont:(UIFont *)deselectedFont {
_deselectedFont = deselectedFont;
[self setupFonts];
}
- (void)setDeselectedFontColor:(UIColor *)deselectedFontColor {
_deselectedFontColor = deselectedFontColor;
[self setupFonts];
}
- (void)setDisabledFont:(UIFont *)disabledFont {
_disabledFont = disabledFont;
[self setupFonts];
}
- (void)setDisabledFontColor:(UIColor *)disabledFontColor {
_disabledFontColor = disabledFontColor;
[self setupFonts];
}
- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
[self configureFlatSegmentedControl];
}
- (void)setBorderWidth:(CGFloat)borderWidth {
_borderWidth = borderWidth;
[self configureFlatSegmentedControl];
}
- (void)setupFonts {
NSDictionary *selectedAttributesDictionary;
if ([[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending) {
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeZero];
[shadow setShadowColor:[UIColor clearColor]];
selectedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.selectedFontColor,
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
self.selectedFont,
NSFontAttributeName,
nil];
} else {
// Pre-iOS6 methods
selectedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.selectedFontColor,
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
self.selectedFont,
UITextAttributeFont,
nil];
}
[self setTitleTextAttributes:selectedAttributesDictionary forState:UIControlStateSelected];
NSDictionary *deselectedAttributesDictionary;
if ([[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending) {
// iOS6+ methods
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeZero];
[shadow setShadowColor:[UIColor clearColor]];
deselectedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.deselectedFontColor,
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
self.deselectedFont,
NSFontAttributeName,
nil];
} else {
// pre-iOS6 methods
deselectedAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.deselectedFontColor,
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
self.deselectedFont,
UITextAttributeFont,
nil];
}
[self setTitleTextAttributes:deselectedAttributesDictionary forState:UIControlStateNormal];
NSDictionary *disabledAttributesDictionary;
if ([[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending) {
// iOS6+ methods
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeZero];
[shadow setShadowColor:[UIColor clearColor]];
disabledAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.disabledFontColor,
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
self.disabledFont,
NSFontAttributeName,
nil];
} else {
// pre-iOS6 methods
disabledAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.disabledFontColor,
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
UITextAttributeTextShadowOffset,
self.disabledFont,
UITextAttributeFont,
nil];
}
[self setTitleTextAttributes:disabledAttributesDictionary forState:UIControlStateDisabled];
}
- (void)configureFlatSegmentedControl {
UIImage *selectedBackgroundImage = [UIImage buttonImageWithColor:self.selectedColor
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *deselectedBackgroundImage = [UIImage buttonImageWithColor:self.deselectedColor
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *disabledBackgroundImage = [UIImage buttonImageWithColor:self.disabledColor
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIColor *selectedColor = (self.dividerColor) ? self.dividerColor : self.selectedColor;
UIColor *deselectedColor = (self.dividerColor) ? self.dividerColor : self.deselectedColor;
UIImage *selectedDividerImage = [[UIImage imageWithColor:selectedColor cornerRadius:0] imageWithMinimumSize:CGSizeMake(1, 1)];
UIImage *deselectedDividerImage = [[UIImage imageWithColor:deselectedColor cornerRadius:0] imageWithMinimumSize:CGSizeMake(1, 1)];
[self setBackgroundImage:selectedBackgroundImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:deselectedBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:disabledBackgroundImage forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[self setDividerImage:deselectedDividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setDividerImage:selectedDividerImage forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setDividerImage:deselectedDividerImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self setDividerImage:selectedDividerImage forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
self.layer.borderWidth = self.borderWidth;
self.layer.borderColor = self.borderColor.CGColor;
self.layer.cornerRadius = self.cornerRadius;
}
@end