-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSKTintedListController.m
281 lines (241 loc) · 11.1 KB
/
SKTintedListController.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
#import <Social/SLComposeViewController.h>
#import <Social/SLServiceTypes.h>
#import "SKTintedListController.h"
#import "common.h"
#import "SKSpecifierParser.h"
@interface PSListController (did_rotate_from_orientation_settingskit)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
@end
@implementation SKTintedListController
- (id)specifiers {
if(_specifiers == nil)
{
if ([self respondsToSelector:@selector(customSpecifiers)])
{
_specifiers = [SKSpecifierParser specifiersFromArray:self.customSpecifiers forTarget:self];
if ([self respondsToSelector:@selector(customTitle)])
self.title = SK_LCL(self.customTitle);
}
else if ([self respondsToSelector:@selector(plistName)])
_specifiers = [self loadSpecifiersFromPlistName:self.plistName target:self];
else
@throw [[NSException alloc] init];
[self localizedSpecifiersWithSpecifiers:_specifiers];
}
return _specifiers;
}
- (id)navigationTitle {
return [[self bundle] localizedStringForKey:[super title] value:[super title] table:nil];
}
- (id)localizedSpecifiersWithSpecifiers:(NSArray *)specifiers {
NSLog(@"localizedSpecifiersWithSpecifiers");
for(PSSpecifier *curSpec in specifiers) {
NSString *name = [curSpec name];
if(name) {
[curSpec setName:[[self bundle] localizedStringForKey:name value:name table:nil]];
}
NSString *footerText = [curSpec propertyForKey:@"footerText"];
if(footerText)
[curSpec setProperty:[[self bundle] localizedStringForKey:footerText value:footerText table:nil] forKey:@"footerText"];
id titleDict = [curSpec titleDictionary];
if(titleDict) {
NSMutableDictionary *newTitles = [[NSMutableDictionary alloc] init];
for(NSString *key in titleDict) {
NSString *value = [titleDict objectForKey:key];
[newTitles setObject:[[self bundle] localizedStringForKey:value value:value table:nil] forKey: key];
}
[curSpec setTitleDictionary:newTitles];
}
}
return specifiers;
}
-(NSString*)localizedString:(NSString*)string
{
return [[self bundle] localizedStringForKey:string value:string table:nil] ?: string;
}
-(void)loadView
{
[super loadView];
if (self.showHeartImage)
{
UIImage* image = [UIImage imageNamed:@"heart.png" inBundle:[NSBundle bundleForClass:SKTintedListController.class]];
if ([self respondsToSelector:@selector(heartImageColor)])
image = [image changeImageColor:self.heartImageColor];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(heartWasTouched) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *heartButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];
if ([self respondsToSelector:@selector(shiftHeartImage)] && self.shiftHeartImage)
{
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -16;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, heartButton, nil] animated:NO];
}
else
((UINavigationItem*)self.navigationItem).rightBarButtonItem = heartButton;
}
BOOL tintSwitches_ = YES;
if ([self respondsToSelector:@selector(tintSwitches)])
tintSwitches_ = [self tintSwitches];
if (tintSwitches_)
{
if ([self respondsToSelector:@selector(switchOnTintColor)])
{
if (SK_SYSTEM_VERSION_LESS_THAN(@"9.0"))
[UISwitch appearanceWhenContainedIn:self.class,nil].onTintColor = self.switchOnTintColor;
else
[UISwitch appearanceWhenContainedInInstancesOfClasses:@[self.class]].onTintColor = self.switchOnTintColor;
}
else
{
if ([self respondsToSelector:@selector(tintColor)])
{
if (SK_SYSTEM_VERSION_LESS_THAN(@"9.0"))
[UISwitch appearanceWhenContainedIn:self.class,nil].onTintColor = self.tintColor;
else
[UISwitch appearanceWhenContainedInInstancesOfClasses:@[self.class]].onTintColor = self.tintColor;
}
}
if ([self respondsToSelector:@selector(switchTintColor)])
{
if (SK_SYSTEM_VERSION_LESS_THAN(@"9.0"))
[UISwitch appearanceWhenContainedIn:self.class,nil].onTintColor = self.switchTintColor;
else
[UISwitch appearanceWhenContainedInInstancesOfClasses:@[self.class]].onTintColor = self.switchTintColor;
}
else
if ([self respondsToSelector:@selector(tintColor)])
{
if (SK_SYSTEM_VERSION_LESS_THAN(@"9.0"))
[UISwitch appearanceWhenContainedIn:self.class,nil].onTintColor = self.tintColor;
else
[UISwitch appearanceWhenContainedInInstancesOfClasses:@[self.class]].onTintColor = self.tintColor;
}
}
}
-(void) heartWasTouched
{
SLComposeViewController *composeController = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
if ([self respondsToSelector:@selector(shareMessage)])
[composeController setInitialText:self.shareMessage];
else
[composeController setInitialText:@"Someone needs to change their [SKTintedListController shareMessage]!"];
[self presentViewController:composeController
animated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setupHeader];
if ([self respondsToSelector:@selector(tintColor)])
{
self.view.tintColor = self.tintColor;
[[UIApplication sharedApplication] keyWindow].tintColor = self.tintColor;
}
if ([self respondsToSelector:@selector(navigationTintColor)])
{
self.navigationController.navigationBar.tintColor = self.navigationTintColor;
[[UIApplication sharedApplication] keyWindow].tintColor = self.navigationTintColor;
}
else
if ([self respondsToSelector:@selector(tintColor)])
self.navigationController.navigationBar.tintColor = self.tintColor;
BOOL tintNavText = YES;
if ([self respondsToSelector:@selector(tintNavigationTitleText)])
tintNavText = self.tintNavigationTitleText;
if (tintNavText)
{
if ([self respondsToSelector:@selector(navigationTitleTintColor)])
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: self. navigationTitleTintColor };
else
if ([self respondsToSelector:@selector(tintColor)])
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: self.tintColor };
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] keyWindow].tintColor = nil;
self.view.tintColor = nil;
self.navigationController.navigationBar.tintColor = nil;
self.navigationController.navigationBar.titleTextAttributes = @{};
}
-(void) setupHeader
{
UIView *header = nil;
if ([self respondsToSelector:@selector(headerImage)])
{
header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
UIImage *headerImage = [UIImage imageNamed:self.headerImage inBundle:[NSBundle bundleForClass:self.class]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:headerImage];
header.frame = (CGRect){ header.frame.origin, headerImage.size };
imageView.frame = CGRectMake(imageView.frame.origin.x, 10, imageView.frame.size.width, headerImage.size.height);
[header addSubview:imageView];
}
if ([self respondsToSelector:@selector(headerText)])
{
header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, /*UIScreen.mainScreen.bounds.size.width*/self.view.frame.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 17, header.frame.size.width, header.frame.size.height - 10)];
label.text = SK_LCL(self.headerText);
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:48];
label.backgroundColor = [UIColor clearColor];
if ([self respondsToSelector:@selector(tintColor)])
label.textColor = self.tintColor;
if ([self respondsToSelector:@selector(headerColor)])
label.textColor = self.headerColor;
label.textAlignment = NSTextAlignmentCenter;
if ([self respondsToSelector:@selector(headerSubText)])
{
header.frame = CGRectMake(header.frame.origin.x, header.frame.origin.y, header.frame.size.width, header.frame.size.height + 35);
label.frame = CGRectMake(label.frame.origin.x, 10, label.frame.size.width, label.frame.size.height - 5);
[header addSubview:label];
UILabel *subText = [[UILabel alloc] initWithFrame:CGRectMake(header.frame.origin.x, label.frame.origin.y + label.frame.size.height, header.frame.size.width, 20)];
subText.text = SK_LCL(self.headerSubText);
subText.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16];
subText.backgroundColor = [UIColor clearColor];
if ([self respondsToSelector:@selector(tintColor)])
subText.textColor = self.tintColor;
if ([self respondsToSelector:@selector(headerColor)])
subText.textColor = self.headerColor;
subText.textAlignment = NSTextAlignmentCenter;
[header addSubview:subText];
}
else
{
label.frame = CGRectMake(label.frame.origin.x, 5, label.frame.size.width, label.frame.size.height);
[header addSubview:label];
}
}
if ([self respondsToSelector:@selector(headerView)])
{
header = self.headerView;
}
else if (header)
{
header.backgroundColor = [UIColor clearColor];
header.autoresizesSubviews = YES;
header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
if (header)
{
[self.table setTableHeaderView:header];
}
}
-(BOOL) showHeartImage { return YES; }
-(BOOL) shiftHeartImage { return YES; }
-(UIColor*) heartImageColor
{
if ([self respondsToSelector:@selector(navigationTintColor)])
return self.navigationTintColor;
else if ([self respondsToSelector:@selector(tintColor)])
return self.tintColor;
else
return SK_SYSTEM_TINT;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self setupHeader];
}
@end