-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLCustomDrawnTableViewCell.m
230 lines (189 loc) · 8.11 KB
/
TLCustomDrawnTableViewCell.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
//
// TLCustomDrawnTableViewCell.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 9/8/09.
//
#import "TLCustomDrawnTableViewCell.h"
#import "CGGeometry_TLCommon.h"
#import "CGContext_TLCommon.h"
#import "TLMacros.h"
#pragma mark -
@interface TLCustomDrawnTableViewCellCustomView : UIView {
TLCustomDrawnTableViewCell *parentCell;
}
@property(nonatomic, assign, readwrite) TLCustomDrawnTableViewCell *parentCell;
@end
#pragma mark -
@implementation TLCustomDrawnTableViewCellCustomView
@synthesize parentCell;
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
self.opaque = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if(self.parentCell.selected || self.parentCell.highlighted) {
// Paint a white background first to show through, so any alpha does something
if(self.parentCell.selectedCellColor) {
[[UIColor whiteColor] setFill];
UIRectFill(rect);
[self.parentCell.selectedCellColor setFill];
UIRectFillUsingBlendMode(rect, kCGBlendModeMultiply);
}
} else {
if(self.parentCell.cellColor) {
[[UIColor whiteColor] setFill];
UIRectFill(rect);
[self.parentCell.cellColor setFill];
UIRectFillUsingBlendMode(rect, kCGBlendModeMultiply);
}
}
CGContextRestoreGState(context);
[self.parentCell drawContentsInRect:rect];
}
@end
#pragma mark -
@implementation TLCustomDrawnTableViewCell
@synthesize customView;
+ (TLTableViewCell *)tableViewCellWithStyle:(UITableViewCellStyle)style
dequeuedIfPossibleFromTableView:(UITableView *)tableView {
return [self tableViewCellDequeuedIfPossibleFromTableView:tableView
reuseIdentifier:[self defaultReuseIdentifier]
keyPathsRequiringRedisplay:nil];
}
+ (TLTableViewCell *)tableViewCellWithStyle:(UITableViewCellStyle)style
dequeuedIfPossibleFromTableView:(UITableView *)tableView
reuseIdentifier:(NSString *)reuseIdentifier {
return [self tableViewCellDequeuedIfPossibleFromTableView:tableView
reuseIdentifier:reuseIdentifier
keyPathsRequiringRedisplay:nil];
}
+ (TLCustomDrawnTableViewCell *)tableViewCellDequeuedIfPossibleFromTableView:(UITableView *)tableView
keyPathsRequiringRedisplay:(NSSet *)redisplayRequiringKeyPathsOrNil {
return [self tableViewCellDequeuedIfPossibleFromTableView:tableView
reuseIdentifier:[self defaultReuseIdentifier]
keyPathsRequiringRedisplay:redisplayRequiringKeyPathsOrNil];
}
+ (TLCustomDrawnTableViewCell *)tableViewCellDequeuedIfPossibleFromTableView:(UITableView *)tableView
reuseIdentifier:(NSString *)reuseIdentifier
keyPathsRequiringRedisplay:(NSSet *)redisplayRequiringKeyPathsOrNil {
TLCustomDrawnTableViewCell *cell = (TLCustomDrawnTableViewCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell) {
cell = [[[self alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reuseIdentifier
keyPathsRequiringRedisplay:redisplayRequiringKeyPathsOrNil]
autorelease];
}
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier keyPathsRequiringRedisplay:(NSSet *)redisplayRequiringKeyPathsOrNil {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
CGRect customViewFrame = CGRectZeroWithSize(self.contentView.bounds.size);
customView = [[[TLCustomDrawnTableViewCellCustomView alloc] initWithFrame:customViewFrame] autorelease];
((TLCustomDrawnTableViewCellCustomView *)customView).parentCell = self;
[self.contentView addSubview:self.customView];
keyPathsRequiringRedisplay = [redisplayRequiringKeyPathsOrNil retain];
for(NSString *keyPath in keyPathsRequiringRedisplay) {
[self addObserver:self forKeyPath:keyPath options:0 context:NULL];
}
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if((self == object) && [keyPathsRequiringRedisplay containsObject:keyPath]) {
[self.customView setNeedsDisplay];
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
[self.customView setNeedsDisplay];
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
[self.customView setNeedsDisplay];
}
- (void)setNeedsDisplay {
[super setNeedsDisplay];
[self.customView setNeedsDisplay];
}
- (void)drawContentsInRect:(CGRect)rect {
// subclasses override this
}
- (void)dealloc {
for(NSString *keyPath in keyPathsRequiringRedisplay) {
[self removeObserver:self forKeyPath:keyPath];
}
[keyPathsRequiringRedisplay release], keyPathsRequiringRedisplay = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Drawing utilities for subclasses' use
- (CGFloat)drawLeftImage:(UIImage *)image
size:(CGSize)size
leftPadding:(CGFloat)leftPadding
rightPadding:(CGFloat)rightPadding
cornerRadius:(CGFloat)cornerRadius
contentMode:(UIViewContentMode)contentMode {
// Calculate two rects: The image drawing rect, and
// the clipping rect.
CGRect clippingRect = CGRectWithXYAndSize(leftPadding,
OffsetToCenterFloatInFloat(size.height, self.customView.bounds.size.height),
size);
clippingRect = CGRectRoundedToNearestPixel(clippingRect);
// some handy precalculations
CGFloat destinationAspectRatio = size.width / size.height;
CGFloat imageAspectRatio = image.size.width / image.size.height;
CGSize imageDrawingSize = CGSizeZero;
switch(contentMode) {
case UIViewContentModeScaleToFill:;
imageDrawingSize = size;
break;
case UIViewContentModeScaleAspectFit:;
CGFloat fitScalingFactor = 1;
if(destinationAspectRatio > imageAspectRatio) {
fitScalingFactor = size.height / image.size.height;
} else {
fitScalingFactor = size.width / image.size.width;
}
imageDrawingSize = ScaledSize(image.size, fitScalingFactor);
break;
case UIViewContentModeScaleAspectFill:;
CGFloat fillScalingFactor = 1;
if(destinationAspectRatio > imageAspectRatio) {
fillScalingFactor = size.width / image.size.width;
} else {
fillScalingFactor = size.height / image.size.height;
}
imageDrawingSize = ScaledSize(image.size, fillScalingFactor);
break;
default:;
TLDebugLog(@"Content mode %i not supported!", contentMode);
break;
}
// Now that we have the size, pick a drawing rect that centers the drawn image size in its clipping rect
CGRect imageDrawingRect = CenteredRectInRectWithSize(clippingRect, imageDrawingSize);
imageDrawingRect = CGRectRoundedToNearestPixel(imageDrawingRect);
if(contentMode == UIViewContentModeScaleAspectFit) {
// reduce clipping rect to actual image drawing rect, so that the rounded corners do their thing;
// in the other modes, we've scaled the image up to meet the clipping rect, so the clipping rect
// doesn't need to be modified.
clippingRect = imageDrawingRect;
}
// Set up the rounded corners and draw!
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextAddRoundedRectToPath(context, clippingRect, cornerRadius, cornerRadius);
CGContextClosePath(context);
CGContextClip(context);
[image drawInRect:imageDrawingRect];
CGContextRestoreGState(context);
return CGRectGetMaxX(clippingRect) + rightPadding;
}
@end