This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
TTSearchBar.m
333 lines (266 loc) · 9.25 KB
/
TTSearchBar.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
#import "Three20/TTSearchBar.h"
#import "Three20/TTSearchTextField.h"
#import "Three20/TTBackgroundView.h"
#import "Three20/TTAppearance.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
static const CGFloat kMarginX = 5;
static const CGFloat kMarginY = 6;
static const CGFloat kPaddingX = 10;
static const CGFloat kPaddingY = 10;
static const CGFloat kSpacingX = 5;
static const CGFloat kButtonHeight = 30;
static const CGFloat kIndexViewMargin = 4;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTSearchBar
@synthesize boxView = _boxView, tintColor = _tintColor,
showsCancelButton = _showsCancelButton, showsSearchIcon = _showsSearchIcon;
///////////////////////////////////////////////////////////////////////////////////////////////////
// private
- (CGFloat)indexViewWidth {
UITableView* tableView = (UITableView*)[self firstParentOfClass:[UITableView class]];
if (tableView) {
UIView* indexView = tableView.indexView;
if (indexView) {
return indexView.width;
}
}
return 0;
}
- (void)showIndexView:(BOOL)show {
UITableView* tableView = (UITableView*)[self firstParentOfClass:[UITableView class]];
if (tableView) {
UIView* indexView = tableView.indexView;
if (indexView) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_TRANSITION_DURATION];
if (show) {
CGRect frame = indexView.frame;
frame.origin.x = self.width - (indexView.width + kIndexViewMargin);
indexView.frame = frame;
} else {
indexView.frame = CGRectOffset(indexView.frame, indexView.width + kIndexViewMargin, 0);
}
indexView.alpha = show ? 1 : 0;
CGRect searchFrame = _searchField.frame;
searchFrame.size.width += show ? -self.indexViewWidth : self.indexViewWidth;
_searchField.frame = searchFrame;
CGRect boxFrame = _boxView.frame;
boxFrame.size.width += show ? -self.indexViewWidth : self.indexViewWidth;
_boxView.frame = boxFrame;
[UIView commitAnimations];
}
}
}
- (void)scrollToTop {
UIScrollView* scrollView = (UIScrollView*)[self firstParentOfClass:[UIScrollView class]];
if (scrollView) {
CGPoint offset = scrollView.contentOffset;
if (offset.y != self.top) {
[scrollView setContentOffset:CGPointMake(offset.x, self.top) animated:YES];
}
}
}
- (void)textFieldDidBeginEditing {
[self scrollToTop];
[self showIndexView:NO];
}
- (void)textFieldDidEndEditing {
[self showIndexView:YES];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_style = TTDrawReflection;
_boxView = [[TTBackgroundView alloc] initWithFrame:CGRectZero];
_boxView.backgroundColor = [UIColor clearColor];
_boxView.style = TTDrawRoundInnerShadow;
_boxView.contentMode = UIViewContentModeRedraw;
[self addSubview:_boxView];
_searchField = [[TTSearchTextField alloc] initWithFrame:CGRectZero];
_searchField.placeholder = TTLocalizedString(@"Search", @"");
_searchField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[_searchField addTarget:self action:@selector(textFieldDidBeginEditing)
forControlEvents:UIControlEventEditingDidBegin];
[_searchField addTarget:self action:@selector(textFieldDidEndEditing)
forControlEvents:UIControlEventEditingDidEnd];
[self addSubview:_searchField];
self.contentMode = UIViewContentModeRedraw;
self.tintColor = [TTAppearance appearance].barTintColor;
self.showsSearchIcon = YES;
self.showsCancelButton = NO;
}
return self;
}
- (void)dealloc {
[_searchField release];
[_boxView release];
[_tintColor release];
[_cancelButton release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIResponder
- (BOOL)becomeFirstResponder {
return [_searchField becomeFirstResponder];
}
- (BOOL)resignFirstResponder {
return [_searchField resignFirstResponder];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView
- (void)drawRect:(CGRect)rect {
if (_style == TTDrawReflection) {
UIColor* fill[] = {_tintColor};
[[TTAppearance appearance] draw:TTDrawReflection rect:rect
fill:fill fillCount:1 stroke:nil radius:0];
} else {
[super drawRect:rect];
}
}
- (void)layoutSubviews {
CGFloat indexViewWidth = [_searchField isEditing] ? 0 : self.indexViewWidth;
CGFloat leftPadding = _showsSearchIcon ? 0 : kSpacingX;
CGFloat buttonWidth = 0;
if (_showsCancelButton) {
[_cancelButton sizeToFit];
buttonWidth = _cancelButton.width + kSpacingX;
}
CGFloat boxHeight = TOOLBAR_HEIGHT - kMarginY*2;
CGRect boxRect = CGRectMake(kMarginX, floor(self.height/2 - boxHeight/2),
self.width - kMarginX*2, boxHeight);
boxRect.size.width -= indexViewWidth + buttonWidth;
_boxView.frame = boxRect;
_searchField.frame = CGRectMake(kMarginX+kPaddingX+leftPadding, 0,
self.width - (kMarginX*2+kPaddingX+leftPadding+buttonWidth+indexViewWidth), self.height);
if (_showsCancelButton) {
_cancelButton.frame = CGRectMake(_boxView.right + kSpacingX,
ceil(self.height/2 - kButtonHeight/2),
_cancelButton.width, kButtonHeight);
}
}
- (CGSize)sizeThatFits:(CGSize)size {
CGSize fontSize = [@"M" sizeWithFont:self.font];
return CGSizeMake(size.width, fontSize.height+kPaddingY*2);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// public
- (id<UITextFieldDelegate>)delegate {
return _searchField.delegate;
}
- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
_searchField.delegate = delegate;
}
- (id<TTTableViewDataSource>)dataSource {
return _searchField.dataSource;
}
- (void)setDataSource:(id<TTTableViewDataSource>)dataSource {
_searchField.dataSource = dataSource;
}
- (BOOL)editing {
return _searchField.editing;
}
- (BOOL)showsDoneButton {
return _searchField.showsDoneButton;
}
- (void)setShowsDoneButton:(BOOL)showsDoneButton {
_searchField.showsDoneButton = showsDoneButton;
}
- (BOOL)showsDarkScreen {
return _searchField.showsDarkScreen;
}
- (void)setShowsDarkScreen:(BOOL)showsDarkScreen {
_searchField.showsDarkScreen = showsDarkScreen;
}
- (void)setShowsCancelButton:(BOOL)showsCancelButton {
if (showsCancelButton != _showsCancelButton) {
_showsCancelButton = showsCancelButton;
if (_showsCancelButton) {
_cancelButton = [[UIButton blackButton] retain];
[_cancelButton setTitle:TTLocalizedString(@"Cancel", @"") forState:UIControlStateNormal];
[_cancelButton addTarget:_searchField action:@selector(resignFirstResponder)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_cancelButton];
} else {
[_cancelButton removeFromSuperview];
[_cancelButton release];
_cancelButton = nil;
}
}
}
- (void)setShowsSearchIcon:(BOOL)showsSearchIcon {
if (showsSearchIcon != _showsSearchIcon) {
_showsSearchIcon = showsSearchIcon;
if (_showsSearchIcon) {
UIImageView* iconView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Three20.bundle/images/searchIcon.png"]] autorelease];
[iconView sizeToFit];
iconView.contentMode = UIViewContentModeLeft;
iconView.frame = CGRectInset(iconView.frame, -floor(kMarginX/2), 0);
_searchField.leftView = iconView;
_searchField.leftViewMode = UITextFieldViewModeAlways;
} else {
_searchField.leftView = nil;
_searchField.leftViewMode = UITextFieldViewModeNever;
}
}
}
- (BOOL)searchesAutomatically {
return _searchField.searchesAutomatically;
}
- (void)setSearchesAutomatically:(BOOL)searchesAutomatically {
_searchField.searchesAutomatically = searchesAutomatically;
}
- (NSString*)text {
return _searchField.text;
}
- (void)setText:(NSString*)text {
_searchField.text = text;
}
- (NSString*)placeholder {
return _searchField.placeholder;
}
- (void)setPlaceholder:(NSString*)placeholder {
_searchField.placeholder = placeholder;
}
- (UITableView*)tableView {
return _searchField.tableView;
}
- (void)setTintColor:(UIColor*)tintColor {
if (tintColor != _tintColor) {
[_tintColor release];
_tintColor = [tintColor retain];
_boxView.strokeColor = [_tintColor transformHue:1 saturation:1 value:0.9];
}
}
- (UIColor*)textColor {
return _searchField.textColor;
}
- (void)setTextColor:(UIColor*)textColor {
_searchField.textColor = textColor;
}
- (UIFont*)font {
return _searchField.font;
}
- (void)setFont:(UIFont*)font {
_searchField.font = font;
}
- (CGFloat)rowHeight {
return _searchField.rowHeight;
}
- (void)setRowHeight:(CGFloat)rowHeight {
_searchField.rowHeight = rowHeight;
}
- (UIReturnKeyType)returnKeyType {
return _searchField.returnKeyType;
}
- (void)setReturnKeyType:(UIReturnKeyType)returnKeyType {
_searchField.returnKeyType = returnKeyType;
}
- (void)search {
[_searchField search];
}
- (void)showSearchResults:(BOOL)show {
[_searchField showSearchResults:show];
}
@end