-
Notifications
You must be signed in to change notification settings - Fork 5
/
PWThemePlistParser.m
250 lines (183 loc) · 8.29 KB
/
PWThemePlistParser.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
//
// ProWidgets
//
// 1.0.0
//
// Created by Alan Yip on 18 Jan 2014
// Copyright 2014 Alan Yip. All rights reserved.
//
#import "PWThemePlistParser.h"
#import "PWTheme.h"
#import "PWThemeParsed.h"
static NSArray *boolKeys = nil;
static NSArray *imageKeys = nil;
static NSArray *colorKeys = nil;
static NSArray *doubleKeys = nil;
// convert key to selector name (setXxx:)
static inline SEL NSSetSelectorFromKey(NSString *key) {
NSString *selectorName = [NSString stringWithFormat:@"set%@%@:", [[key substringToIndex:1] uppercaseString], [key substringFromIndex:1]];
return NSSelectorFromString(selectorName);
}
// convert key to selector name (setXxx:forOrientation:)
static inline SEL NSSetImageSelectorFromKey(NSString *key) {
NSString *selectorName = [NSString stringWithFormat:@"set%@%@:forOrientation:", [[key substringToIndex:1] uppercaseString], [key substringFromIndex:1]];
return NSSelectorFromString(selectorName);
}
static inline UIImage *retrieveImage(PWTheme *theme, NSString *name, NSString *capInsets) {
if (theme == nil || name == nil || [name length] == 0) return nil;
// convert cap insets string to UIEdgeInsets
BOOL includeCapInsets = NO;
UIEdgeInsets imageCapInsets = UIEdgeInsetsZero;
if (capInsets != nil && [capInsets length] > 0) {
// ^([\d\.]+)\s*,\s*([\d\.]+)\s*,\s*([\d\.]+)\s*,\s*([\d\.]+)$
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^([\\d\\.]+)\\s*,\\s*([\\d\\.]+)\\s*,\\s*([\\d\\.]+)\\s*,\\s*([\\d\\.]+)$"
options:0
error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:capInsets
options:0
range:NSMakeRange(0, [capInsets length])];
// invalid format
if (match != nil) {
CGFloat top = 0.0;
CGFloat left = 0.0;
CGFloat bottom = 0.0;
CGFloat right = 0.0;
// the first range is the full length of source string
for (unsigned int i = 0; i < [match numberOfRanges] - 1; i++) {
NSRange range = [match rangeAtIndex:i + 1];
if (range.location == NSNotFound) continue;
NSString *part = [capInsets substringWithRange:range];
CGFloat doubleValue = [part doubleValue];
if (i == 0)
top = doubleValue;
else if (i == 1)
left = doubleValue;
else if (i == 2)
bottom = doubleValue;
else if (i == 3)
right = doubleValue;
}
includeCapInsets = YES;
imageCapInsets = UIEdgeInsetsMake(top, left, bottom, right);
}
}
return includeCapInsets ? [theme imageNamed:name withCapInsets:imageCapInsets] : [theme imageNamed:name];
}
static inline void configureCellHeight(PWThemeParsed *theme, NSDictionary *dict, PWWidgetOrientation orientation) {
NSNumber *normalHeight = dict[@"normal"];
NSNumber *textareaHeight = dict[@"textarea"];
if (normalHeight != nil && textareaHeight != nil) {
[theme setHeightOfCell:[normalHeight doubleValue] forType:PWWidgetCellTypeNormal forOrientation:orientation];
[theme setHeightOfCell:[textareaHeight doubleValue] forType:PWWidgetCellTypeTextArea forOrientation:orientation];
}
}
@implementation PWThemePlistParser
+ (void)load {
if (boolKeys != nil && imageKeys != nil && colorKeys != nil && doubleKeys != nil) return;
boolKeys = [@[@"wantsDarkKeyboard"] copy];
imageKeys = [@[@"sheetBackgroundImage", @"navigationBarBackgroundImage", @"cellBackgroundImage", @"cellSelectedBackgroundImage"] copy];
colorKeys = [@[@"tintColor", @"sheetForegroundColor", @"sheetBackgroundColor", @"navigationBarBackgroundColor", @"navigationTitleTextColor", @"navigationButtonTextColor", @"cellSeparatorColor", @"cellBackgroundColor", @"cellTitleTextColor", @"cellValueTextColor", @"cellButtonTextColor", @"cellInputTextColor", @"cellInputPlaceholderTextColor", @"cellPlainTextColor", @"cellSelectedBackgroundColor", @"cellSelectedTitleTextColor", @"cellSelectedValueTextColor", @"cellSelectedButtonTextColor", @"cellHeaderFooterViewBackgroundColor", @"cellHeaderFooterViewTitleTextColor", @"switchThumbColor", @"switchOnColor", @"switchOffColor"] copy];
doubleKeys = [@[@"cornerRadius"] copy];
}
+ (PWTheme *)parse:(NSDictionary *)dict inBundle:(NSBundle *)bundle forWidget:(PWWidget *)widget {
LOG(@"PWThemePlistParser: Parsing theme plist");
PWThemeParsed *theme = [PWThemeParsed new];
// set basic information
theme.name = dict[@"name"];
theme.bundle = bundle;
theme.widget = widget;
// process boolean keys
for (NSString *key in boolKeys) {
NSNumber *value = dict[key];
// not specified in plist
if (value == nil || ![value isKindOfClass:[NSNumber class]]) continue;
// convert key to selector name (setXxx)
SEL selector = NSSetSelectorFromKey(key);
if ([theme respondsToSelector:selector]) {
LOG(@"PWThemePlistParser: set '%@' to %@", key, value);
[theme performSelector:selector withObject:value];
} else {
LOG(@"PWThemePlistParser: selector '%@' not found", NSStringFromSelector(selector));
}
}
// process image keys
for (NSString *key in imageKeys) {
id value = dict[key];
// not specified in plist
if (value == nil || ![value isKindOfClass:[NSDictionary class]]) continue;
SEL selector = NSSetImageSelectorFromKey(key);
if (![theme respondsToSelector:selector]) {
LOG(@"PWThemePlistParser: selector '%@' not found", NSStringFromSelector(selector));
continue;
}
// specified images for two orientations
NSString *portraitImageName = value[@"portrait"];
NSString *landscapeImageName = value[@"landscape"];
// specified image cap insets for two orientations
NSString *portraitCapInsets = value[@"portraitCapInsets"];
NSString *landscapeCapInsets = value[@"landscapeCapInsets"];
// retrieve images for both orientations
UIImage *portraitImage = retrieveImage(theme, portraitImageName, portraitCapInsets);
UIImage *landscapeImage = retrieveImage(theme, landscapeImageName, landscapeCapInsets);
if (portraitImage != nil) {
LOG(@"PWThemePlistParser: set '%@' (portrait) to %@", key, portraitImage);
[theme performSelector:selector withObject:portraitImage withObject:[NSNumber numberWithInt:PWWidgetOrientationPortrait]];
}
if (landscapeImage != nil) {
LOG(@"PWThemePlistParser: set '%@' (landscape) to %@", key, landscapeImage);
[theme performSelector:selector withObject:landscapeImage withObject:[NSNumber numberWithInt:PWWidgetOrientationLandscape]];
}
}
// process color keys
for (NSString *key in colorKeys) {
NSString *value = dict[key];
// not specified in plist
if (value == nil || ![value isKindOfClass:[NSString class]] || [value length] == 0) continue;
// convert color string into UIColor
UIColor *color = [PWTheme parseColorString:value];
if (color != nil) {
SEL selector = NSSetSelectorFromKey(key);
if ([theme respondsToSelector:selector]) {
LOG(@"PWThemePlistParser: set '%@' to %@", key, color);
[theme performSelector:selector withObject:color];
} else {
LOG(@"PWThemePlistParser: selector '%@' not found", NSStringFromSelector(selector));
}
} else {
LOG(@"PWThemePlistParser: unable to parse color string '%@'", value);
}
}
// process double keys
for (NSString *key in doubleKeys) {
NSNumber *value = dict[key];
// not specified in plist
if (value == nil || ![value isKindOfClass:[NSNumber class]]) continue;
// convert key to selector name (setXxx)
SEL selector = NSSetSelectorFromKey(key);
if ([theme respondsToSelector:selector]) {
LOG(@"PWThemePlistParser: set '%@' to %@", key, value);
[theme performSelector:selector withObject:value];
} else {
LOG(@"PWThemePlistParser: selector '%@' not found", NSStringFromSelector(selector));
}
}
// process cell height
NSDictionary *cellHeight = dict[@"cellHeight"];
if (cellHeight != nil) {
NSDictionary *portrait = cellHeight[@"portrait"];
NSDictionary *landscape = cellHeight[@"landscape"];
if (portrait == nil && landscape == nil) {
// root -> normal / textarea
configureCellHeight(theme, cellHeight, PWWidgetOrientationPortrait);
configureCellHeight(theme, cellHeight, PWWidgetOrientationLandscape);
} else {
// root -> portrait/landscape -> normal/textarea
if (portrait != nil)
configureCellHeight(theme, portrait, PWWidgetOrientationPortrait);
if (landscape != nil)
configureCellHeight(theme, landscape, PWWidgetOrientationLandscape);
}
}
return [theme autorelease];
}
@end