-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPWThemableTableViewCell.m
223 lines (163 loc) · 6.13 KB
/
PWThemableTableViewCell.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
//
// ProWidgets
//
// 1.0.0
//
// Created by Alan Yip on 18 Jan 2014
// Copyright 2014 Alan Yip. All rights reserved.
//
#import "PWThemableTableViewCell.h"
#import "PWController.h"
#import "PWTheme.h"
static UIImage *disclosureImage = nil;
@interface UITableViewCell (PrivateTintColor)
- (UIImage *)_disclosureImage:(BOOL)arg;
- (UIButton *)_accessoryView:(BOOL)arg;
@end
@implementation PWThemableTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier theme:(PWTheme *)theme {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
[self setTheme:theme];
}
return self;
}
- (PWTheme *)theme {
return _theme;
}
- (void)setTheme:(PWTheme *)theme {
if (_theme == nil || ![_theme isEqual:theme]) {
[_theme release];
_theme = [theme retain];
RELEASE_VIEW(_customSeparatorView)
_customSeparatorView = [UIView new];
_customSeparatorView.userInteractionEnabled = NO;
[self addSubview:_customSeparatorView];
}
if (_configuredAppearance) {
_configuredAppearance = NO;
[self _configureAppearance];
}
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview != nil)
[self _configureAppearance];
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
_customSeparatorView.frame = CGRectMake(0, size.height - .5, size.width, .5);
_customSeparatorView.backgroundColor = _theme.cellSeparatorColor;
if (self.accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
UIButton *accessoryView = [self _accessoryView:NO];
accessoryView.alpha = .25;
}
}
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType {
UITableViewCellAccessoryType oldType = self.accessoryType;
[super setAccessoryType:accessoryType];
if (accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
if (disclosureImage == nil) {
UIImage *image = [self _disclosureImage:NO];
disclosureImage = [[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] retain];
}
UIButton *accessoryView = [self _accessoryView:NO];
accessoryView.tintColor = [UIColor blackColor];
// set accessory view (make it support tint color)
if (oldType != accessoryType) {
[accessoryView setImage:disclosureImage forState:UIControlStateNormal];
}
}
}
- (void)_configureAppearance {
if (_configuredAppearance) return;
_configuredAppearance = YES;
PWTheme *theme = _theme;
PWWidgetOrientation orientation = [PWController currentOrientation];
// cell tint color
[self setTintColor:[theme tintColor]];
// normal state
[self setBackgroundImage:[theme cellBackgroundImageForOrientation:orientation] forOrientation:orientation];
[self setBackgroundColor:[theme cellBackgroundColor]];
[self setTitleTextColor:[theme cellTitleTextColor]];
[self setValueTextColor:[theme cellValueTextColor]];
[self setButtonTextColor:[theme cellButtonTextColor]];
[self setInputTextColor:[theme cellInputTextColor]];
[self setInputPlaceholderTextColor:[theme cellInputPlaceholderTextColor]];
[self setPlainTextColor:[theme cellPlainTextColor]];
// selected state
[self setSelectedBackgroundImage:[theme cellSelectedBackgroundImageForOrientation:orientation] forOrientation:orientation];
[self setSelectedBackgroundColor:[theme cellSelectedBackgroundColor]];
[self setSelectedTitleTextColor:[theme cellSelectedTitleTextColor]];
[self setSelectedValueTextColor:[theme cellSelectedValueTextColor]];
[self setSelectedButtonTextColor:[theme cellSelectedButtonTextColor]];
// switch
[self setSwitchThumbColor:[theme switchThumbColor]];
[self setSwitchOnColor:[theme switchOnColor]];
[self setSwitchOffColor:[theme switchOffColor]];
}
////////// Normal //////////
- (void)setBackgroundImage:(UIImage *)image forOrientation:(PWWidgetOrientation)orientation {
[super setBackgroundColor:[UIColor clearColor]];
if (self.backgroundView != nil && [self.backgroundView isKindOfClass:[UIImageView class]]) {
// set its image
UIImageView *backgroundView = (UIImageView *)self.backgroundView;
backgroundView.image = image;
} else {
// create a new image view
self.backgroundView = [[[UIImageView alloc] initWithImage:image] autorelease];
}
}
- (void)setBackgroundColor:(UIColor *)color {
//[super setBackgroundColor:[UIColor clearColor]];
if (self.backgroundView == nil) {
self.backgroundView = [[UIImageView new] autorelease];
}
self.backgroundView.backgroundColor = color;
}
- (void)setTitleTextColor:(UIColor *)color {
self.textLabel.textColor = color;
}
- (void)setValueTextColor:(UIColor *)color {
self.detailTextLabel.textColor = color;
}
- (void)setButtonTextColor:(UIColor *)color {}
- (void)setInputTextColor:(UIColor *)color {}
- (void)setInputPlaceholderTextColor:(UIColor *)color {}
- (void)setPlainTextColor:(UIColor *)color {}
////////// Selected //////////
- (void)setSelectedBackgroundImage:(UIImage *)image forOrientation:(PWWidgetOrientation)orientation {
if (self.selectedBackgroundView != nil && [self.selectedBackgroundView isKindOfClass:[UIImageView class]]) {
// set its image
UIImageView *backgroundView = (UIImageView *)self.selectedBackgroundView;
backgroundView.image = image;
} else {
// create a new image view
self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:image] autorelease];
}
}
- (void)setSelectedBackgroundColor:(UIColor *)color {
[super setBackgroundColor:[UIColor clearColor]];
if (self.selectedBackgroundView == nil) {
self.selectedBackgroundView = [[UIImageView new] autorelease];
}
self.selectedBackgroundView.backgroundColor = color;
}
- (void)setSelectedTitleTextColor:(UIColor *)color {
self.textLabel.highlightedTextColor = color;
}
- (void)setSelectedValueTextColor:(UIColor *)color {
self.detailTextLabel.highlightedTextColor = color;
}
- (void)setSelectedButtonTextColor:(UIColor *)color {}
////////// Switch //////////
- (void)setSwitchThumbColor:(UIColor *)color {}
- (void)setSwitchOnColor:(UIColor *)color {}
- (void)setSwitchOffColor:(UIColor *)color {}
//////////////////////////////////////////////////////////////////////
- (void)dealloc {
DEALLOCLOG;
RELEASE(_theme)
RELEASE_VIEW(_customSeparatorView)
[super dealloc];
}
@end