forked from facebookarchive/three20
/
TTPickerTextField.m
393 lines (323 loc) · 11.1 KB
/
TTPickerTextField.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#import "Three20/TTPickerTextField.h"
#import "TTPickerViewCell.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
static NSString* kEmpty = @" ";
static NSString* kSelected = @"`";
static CGFloat kCellPaddingY = 3;
static CGFloat kPaddingX = 8;
static CGFloat kSpacingY = 6;
static CGFloat kPaddingRatio = 1.75;
static CGFloat kClearButtonSize = 38;
static CGFloat kMinCursorWidth = 50;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTPickerTextField
@synthesize cellViews = _cellViews, selectedCell = _selectedCell, lineCount = _lineCount;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_cellViews = [[NSMutableArray alloc] init];
_selectedCell = nil;
_lineCount = 1;
_cursorOrigin = CGPointZero;
self.text = kEmpty;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
self.clearButtonMode = UITextFieldViewModeNever;
self.returnKeyType = UIReturnKeyDone;
self.enablesReturnKeyAutomatically = NO;
[self addTarget:self action:@selector(textFieldDidEndEditing)
forControlEvents:UIControlEventEditingDidEnd];
}
return self;
}
- (void)dealloc {
[_cellViews release];
[super dealloc];
}
//////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)layoutCells {
CGSize fontSize = [@"M" sizeWithFont:self.font];
CGFloat lineIncrement = fontSize.height + kCellPaddingY*2 + kSpacingY;
CGFloat marginY = floor(fontSize.height/kPaddingRatio);
CGFloat marginLeft = self.leftView
? kPaddingX + self.leftView.width + kPaddingX/2
: kPaddingX;
CGFloat marginRight = kPaddingX + (self.rightView ? kClearButtonSize : 0);
_cursorOrigin.x = marginLeft;
_cursorOrigin.y = marginY;
_lineCount = 1;
if (self.width) {
for (TTPickerViewCell* cell in _cellViews) {
[cell sizeToFit];
CGFloat lineWidth = _cursorOrigin.x + cell.frame.size.width + marginRight;
if (lineWidth >= self.width) {
_cursorOrigin.x = marginLeft;
_cursorOrigin.y += lineIncrement;
++_lineCount;
}
cell.frame = CGRectMake(_cursorOrigin.x, _cursorOrigin.y-kCellPaddingY,
cell.width, cell.height);
_cursorOrigin.x += cell.frame.size.width + kPaddingX;
}
CGFloat remainingWidth = self.width - (_cursorOrigin.x + marginRight);
if (remainingWidth < kMinCursorWidth) {
_cursorOrigin.x = marginLeft;
_cursorOrigin.y += lineIncrement;
++_lineCount;
}
}
return _cursorOrigin.y + fontSize.height + marginY;
}
- (void)updateHeight {
CGFloat previousHeight = self.height;
CGFloat newHeight = [self layoutCells];
if (previousHeight && newHeight != previousHeight) {
self.height = newHeight;
[self setNeedsDisplay];
SEL sel = @selector(textFieldDidResize:);
if ([self.delegate respondsToSelector:sel]) {
[self.delegate performSelector:sel withObject:self];
}
[self scrollToVisibleLine:YES];
}
}
- (CGFloat)marginY {
CGSize fontSize = [@"M" sizeWithFont:self.font];
return floor(fontSize.height/kPaddingRatio);
}
- (CGFloat)topOfLine:(int)lineNumber {
if (lineNumber == 0) {
return 0;
} else {
CGFloat lineHeight = [@"M" sizeWithFont:self.font].height;
CGFloat lineSpacing = kCellPaddingY*2 + kSpacingY;
CGFloat marginY = floor(lineHeight/kPaddingRatio);
CGFloat lineTop = marginY + lineHeight*lineNumber + lineSpacing*lineNumber;
return lineTop - lineSpacing;
}
}
- (CGFloat)centerOfLine:(int)lineNumber {
CGFloat lineTop = [self topOfLine:lineNumber];
CGFloat lineHeight = [@"M" sizeWithFont:self.font].height + kCellPaddingY*2 + kSpacingY;
return lineTop + floor(lineHeight/2);
}
- (CGFloat)heightWithLines:(int)lines {
CGFloat lineHeight = [@"M" sizeWithFont:self.font].height;
CGFloat lineSpacing = kCellPaddingY*2 + kSpacingY;
CGFloat marginY = floor(lineHeight/kPaddingRatio);
return marginY + lineHeight*lines + lineSpacing*(lines ? lines-1 : 0) + marginY;
}
- (void)selectLastCell {
self.selectedCell = [_cellViews objectAtIndex:_cellViews.count-1];
}
- (NSString*)labelForObject:(id)object {
NSString* label = nil;
if ([_dataSource respondsToSelector:@selector(tableView:labelForObject:)]) {
label = [_dataSource tableView:_tableView labelForObject:object];
}
return label ? label : [NSString stringWithFormat:@"%@", object];
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)layoutSubviews {
if (_dataSource) {
[self layoutCells];
} else {
_cursorOrigin.x = kPaddingX;
_cursorOrigin.y = [self marginY];
if (self.leftView) {
_cursorOrigin.x += self.leftView.width + kPaddingX/2;
}
}
[super layoutSubviews];
}
- (CGSize)sizeThatFits:(CGSize)size {
[self layoutIfNeeded];
CGFloat height = [self heightWithLines:_lineCount];
return CGSizeMake(size.width, height);
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (_dataSource) {
UITouch* touch = [touches anyObject];
if (touch.view == self) {
self.selectedCell = nil;
} else {
if ([touch.view isKindOfClass:[TTPickerViewCell class]]) {
self.selectedCell = (TTPickerViewCell*)touch.view;
}
}
}
[super touchesEnded:touches withEvent:event];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UITextField
- (void)setText:(NSString*)text {
if (_dataSource) {
[self updateHeight];
}
[super setText:text];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (_dataSource && [self.text isEqualToString:kSelected]) {
return CGRectMake(0, 0, 0, 0);
} else {
CGRect frame = CGRectOffset(bounds, _cursorOrigin.x, _cursorOrigin.y);
frame.size.width -= (_cursorOrigin.x + kPaddingX + (self.rightView ? kClearButtonSize : 0));
return frame;
}
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds {
if (self.leftView) {
return CGRectMake(
bounds.origin.x+kPaddingX, self.marginY,
self.leftView.frame.size.width, self.leftView.frame.size.height);
} else {
return bounds;
}
}
- (CGRect)rightViewRectForBounds:(CGRect)bounds {
if (self.rightView) {
return CGRectMake(bounds.size.width - kClearButtonSize, bounds.size.height - kClearButtonSize,
kClearButtonSize, kClearButtonSize);
} else {
return bounds;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTSearchTextField
- (BOOL)hasText {
return self.text.length && ![self.text isEqualToString:kEmpty]
&& ![self.text isEqualToString:kSelected];
}
- (void)showSearchResults:(BOOL)show {
[super showSearchResults:show];
if (show) {
[self scrollToEditingLine:YES];
} else {
[self scrollToVisibleLine:YES];
}
}
- (CGRect)rectForSearchResults:(BOOL)withKeyboard {
UIView* superview = self.superviewForSearchResults;
CGFloat y = superview.screenY;
CGFloat visibleHeight = [self heightWithLines:1];
CGFloat keyboardHeight = withKeyboard ? KEYBOARD_HEIGHT : 0;
CGFloat tableHeight = self.window.height - (y + visibleHeight + keyboardHeight);
return CGRectMake(0, self.bottom-1, superview.frame.size.width, tableHeight+1);
}
- (BOOL)shouldUpdate:(BOOL)emptyText {
if (emptyText && !self.hasText && !self.selectedCell && self.cells.count) {
[self selectLastCell];
return NO;
} else if (emptyText && self.selectedCell) {
[self removeSelectedCell];
[super shouldUpdate:emptyText];
return NO;
} else {
return [super shouldUpdate:emptyText];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UITableViewDelegate
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
id object = [_dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
[self addCellWithObject:object];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIControlEvents
- (void)textFieldDidEndEditing {
if (_selectedCell) {
self.selectedCell = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSArray*)cells {
NSMutableArray* cells = [NSMutableArray array];
for (TTPickerViewCell* cellView in _cellViews) {
[cells addObject:cellView.object ? cellView.object : [NSNull null]];
}
return cells;
}
- (void)addCellWithObject:(id)object {
TTPickerViewCell* cell = [[[TTPickerViewCell alloc] initWithFrame:CGRectZero] autorelease];
NSString* label = [self labelForObject:object];
cell.object = object;
cell.label = label;
cell.font = self.font;
[_cellViews addObject:cell];
[self addSubview:cell];
// Reset text so the cursor moves to be at the end of the cellViews
self.text = kEmpty;
SEL sel = @selector(textField:didAddCellAtIndex:);
if ([self.delegate respondsToSelector:sel]) {
[self.delegate performSelector:sel withObject:self withObject:(id)_cellViews.count-1];
}
}
- (void)removeCellWithObject:(id)object {
for (int i = 0; i < _cellViews.count; ++i) {
TTPickerViewCell* cell = [_cellViews objectAtIndex:i];
if (cell.object == object) {
[_cellViews removeObjectAtIndex:i];
[cell removeFromSuperview];
SEL sel = @selector(textField:didRemoveCellAtIndex:);
if ([self.delegate respondsToSelector:sel]) {
[self.delegate performSelector:sel withObject:self withObject:(id)i];
}
break;
}
}
// Reset text so the cursor oves to be at the end of the cellViews
self.text = self.text;
}
- (void)removeAllCells {
while (_cellViews.count) {
TTPickerViewCell* cell = [_cellViews objectAtIndex:0];
[cell removeFromSuperview];
[_cellViews removeObjectAtIndex:0];
}
_selectedCell = nil;
}
- (void)setSelectedCell:(TTPickerViewCell*)cell {
if (_selectedCell) {
_selectedCell.selected = NO;
}
_selectedCell = cell;
if (_selectedCell) {
_selectedCell.selected = YES;
self.text = kSelected;
} else if (self.cells.count) {
self.text = kEmpty;
}
}
- (void)removeSelectedCell {
if (_selectedCell) {
[self removeCellWithObject:_selectedCell.object];
_selectedCell = nil;
if (_cellViews.count) {
self.text = kEmpty;
} else {
self.text = @"";
}
}
}
- (void)scrollToVisibleLine:(BOOL)animated {
if (self.editing) {
UIScrollView* scrollView = (UIScrollView*)[self firstParentOfClass:[UIScrollView class]];
if (scrollView) {
[scrollView setContentOffset:CGPointMake(0, self.top) animated:animated];
}
}
}
- (void)scrollToEditingLine:(BOOL)animated {
UIScrollView* scrollView = (UIScrollView*)[self firstParentOfClass:[UIScrollView class]];
if (scrollView) {
CGFloat offset = _lineCount == 1 ? 0 : [self topOfLine:_lineCount-1];
[scrollView setContentOffset:CGPointMake(0, self.top+offset) animated:animated];
}
}
@end