forked from escoz/QuickDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QEntryTableViewCell.m
325 lines (258 loc) · 13 KB
/
QEntryTableViewCell.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
//
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#import "QEntryTableViewCell.h"
#import "QuickDialog.h"
#import "QTextField.h"
@interface QEntryTableViewCell ()
- (void)handleActionBarPreviousNext:(UISegmentedControl *)control;
@end
@implementation QEntryTableViewCell {
UISegmentedControl *_prevNext;
}
@synthesize textField = _textField;
-(UIToolbar *)createActionBar {
UIToolbar *actionBar = [[UIToolbar alloc] init];
[actionBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"")
style:UIBarButtonItemStyleDone target:self
action:@selector(handleActionBarDone:)];
_prevNext = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
_prevNext.momentary = YES;
_prevNext.segmentedControlStyle = UISegmentedControlStyleBar;
_prevNext.tintColor = actionBar.tintColor;
[_prevNext addTarget:self action:@selector(handleActionBarPreviousNext:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *prevNextWrapper = [[UIBarButtonItem alloc] initWithCustomView:_prevNext];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[actionBar setItems:[NSArray arrayWithObjects:prevNextWrapper, flexible, doneButton, nil]];
return actionBar;
}
- (void)createSubviews {
self.textField = [[QTextField alloc] init];
[self setNeedsLayout];
}
- (void)setTextField:(UITextField *)textField
{
if (_textField!=nil){
[_textField removeTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
[_textField removeFromSuperview];
}
_textField = textField;
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
_textField.borderStyle = UITextBorderStyleNone;
_textField.delegate = self;
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
_textField.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[_textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
[self.contentView addSubview:_textField];
}
- (QEntryTableViewCell *)init {
self = [self initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"QuickformEntryElement"];
if (self!=nil){
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self createSubviews];
}
return self;
}
- (CGRect)calculateFrameForEntryElement {
int extra = (self.textField.clearButtonMode == UITextFieldViewModeNever) ? 15 :10;
if (_entryElement.title == NULL && _entryElement.image==NULL) {
return CGRectMake(10,10,self.contentView.frame.size.width-10-extra, self.frame.size.height-20);
}
if (_entryElement.title == NULL && _entryElement.image!=NULL){
self.imageView.image = _entryElement.image;
[self.imageView sizeToFit];
return CGRectMake(CGRectGetMaxX(self.imageView.frame) + 10, 10, self.contentView.frame.size.width-10-self.imageView.frame.size.width-extra , self.frame.size.height-20);
}
CGFloat totalWidth = self.contentView.frame.size.width;
CGFloat titleWidth = 0;
if (CGRectEqualToRect(CGRectZero, _entryElement.parentSection.entryPosition)) {
for (QElement *el in _entryElement.parentSection.elements){
if ([el isKindOfClass:[QEntryElement class]]){
QEntryElement *q = (QEntryElement*)el;
CGFloat imageWidth = q.image == NULL ? 0 : self.imageView.frame.size.width;
CGFloat fontSize = self.textLabel.font.pointSize == 0? 17 : self.textLabel.font.pointSize;
CGSize size = [((QEntryElement *)el).title sizeWithFont:[self.textLabel.font fontWithSize:fontSize] forWidth:CGFLOAT_MAX lineBreakMode:NSLineBreakByWordWrapping] ;
CGFloat width = size.width + imageWidth + 20;
if (width>titleWidth)
titleWidth = width;
}
}
int inset = 0;
if ([self respondsToSelector:@selector(separatorInset)]) {
inset = self.separatorInset.left;
};
_entryElement.parentSection.entryPosition = CGRectMake(titleWidth+20,10,totalWidth-titleWidth-_entryElement.appearance.cellBorderWidth-extra-inset, self.frame.size.height-20);
}
return _entryElement.parentSection.entryPosition;
}
- (void)updatePrevNextStatus {
[_prevNext setEnabled:[_entryElement.parentSection.rootElement findElementToFocusOnBefore:_entryElement]!=nil forSegmentAtIndex:0];
[_prevNext setEnabled:[_entryElement.parentSection.rootElement findElementToFocusOnAfter:_entryElement]!=nil forSegmentAtIndex:1];
}
- (void)prepareForElement:(QEntryElement *)element inTableView:(QuickDialogTableView *)tableView{
[self applyAppearanceForElement:element];
self.textLabel.text = element.title;
self.labelingPolicy = element.labelingPolicy;
_quickformTableView = tableView;
_entryElement = element;
_textField.text = _entryElement.textValue;
_textField.placeholder = _entryElement.placeholder;
if ([_textField isKindOfClass:[QTextField class]]) {
QTextField *qtf = (QTextField *) _textField;
qtf.prefix = _entryElement.prefix;
qtf.suffix = _entryElement.suffix;
}
_textField.autocapitalizationType = _entryElement.autocapitalizationType;
_textField.autocorrectionType = _entryElement.autocorrectionType;
_textField.keyboardType = _entryElement.keyboardType;
_textField.keyboardAppearance = _entryElement.keyboardAppearance;
_textField.secureTextEntry = _entryElement.secureTextEntry;
_textField.clearsOnBeginEditing = _entryElement.clearsOnBeginEditing;
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_textField.textAlignment = _entryElement.appearance.entryAlignment;
_textField.returnKeyType = _entryElement.returnKeyType;
_textField.enablesReturnKeyAutomatically = _entryElement.enablesReturnKeyAutomatically;
self.accessoryType = _entryElement.accessoryType;
if (_entryElement.hiddenToolbar){
_textField.inputAccessoryView = nil;
} else if (_textField!=nil){
UIToolbar *toolbar = [self createActionBar];
toolbar.barStyle = element.appearance.toolbarStyle;
toolbar.translucent = element.appearance.toolbarTranslucent;
_textField.inputAccessoryView = toolbar;
}
[self updatePrevNextStatus];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self recalculateEntryFieldPosition];
}
-(void)recalculateEntryFieldPosition {
_entryElement.parentSection.entryPosition = CGRectZero;
CGRect textFieldFrame = [self calculateFrameForEntryElement];
CGRect labelFrame = self.textLabel.frame;
self.textLabel.frame = CGRectMake(labelFrame.origin.x, labelFrame.origin.y,
textFieldFrame.origin.x - labelFrame.origin.x, labelFrame.size.height);
_textField.frame = CGRectMake(textFieldFrame.origin.x, textFieldFrame.origin.y, self.contentView.bounds.size.width - textFieldFrame.origin.x - 20, textFieldFrame.size.height);
}
- (void)prepareForReuse {
_quickformTableView = nil;
_entryElement = nil;
}
- (void)textFieldEditingChanged:(UITextField *)textFieldEditingChanged {
_entryElement.textValue = _textField.text;
[_entryElement handleEditingChanged:self];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 50 * USEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[_quickformTableView scrollToRowAtIndexPath:[_entryElement getIndexPath] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
});
if (_textField.returnKeyType == UIReturnKeyDefault) {
UIReturnKeyType returnType = ([_entryElement.parentSection.rootElement findElementToFocusOnAfter:_entryElement]!=nil) ? UIReturnKeyNext : UIReturnKeyDone;
_textField.returnKeyType = returnType;
}
if(_entryElement && _entryElement.delegate && [_entryElement.delegate respondsToSelector:@selector(QEntryDidBeginEditingElement:andCell:)]){
[_entryElement.delegate QEntryDidBeginEditingElement:_entryElement andCell:self];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
_entryElement.textValue = _textField.text;
if(_entryElement && _entryElement.delegate && [_entryElement.delegate respondsToSelector:@selector(QEntryDidEndEditingElement:andCell:)]){
[_entryElement.delegate QEntryDidEndEditingElement:_entryElement andCell:self];
}
[_entryElement performSelector:@selector(fieldDidEndEditing)];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength > [textField.text length]) {
if (0 != _entryElement.maxLength && textField.text.length >= _entryElement.maxLength) {
return NO;
}
}
if(_entryElement && _entryElement.delegate && [_entryElement.delegate respondsToSelector:@selector(QEntryShouldChangeCharactersInRange:withString:forElement:andCell:)]){
return [_entryElement.delegate QEntryShouldChangeCharactersInRange:range withString:string forElement:_entryElement andCell:self];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
QEntryElement *element = [_entryElement.parentSection.rootElement findElementToFocusOnAfter:_entryElement];
if (element!=nil){
UITableViewCell *cell = [_quickformTableView cellForElement:element];
if (cell!=nil){
[cell becomeFirstResponder];
}
} else {
[_textField resignFirstResponder];
}
if(_entryElement && _entryElement.delegate && [_entryElement.delegate respondsToSelector:@selector(QEntryShouldReturnForElement:andCell:)]){
return [_entryElement.delegate QEntryShouldReturnForElement:_entryElement andCell:self];
}
return YES;
}
- (void)handleActionBarPreviousNext:(UISegmentedControl *)control {
QEntryElement *element;
const BOOL isNext = control.selectedSegmentIndex == 1;
if (isNext) {
element = [_entryElement.parentSection.rootElement findElementToFocusOnAfter:_entryElement];
} else {
element = [_entryElement.parentSection.rootElement findElementToFocusOnBefore:_entryElement];
}
if (element != nil) {
UITableViewCell *cell = [_quickformTableView cellForElement:element];
if (cell != nil) {
[cell becomeFirstResponder];
}
else {
[_quickformTableView scrollToRowAtIndexPath:[element getIndexPath]
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
UITableViewCell *c = [_quickformTableView cellForElement:element];
if (c != nil) {
[c becomeFirstResponder];
}
});
}
}
if (_entryElement.keepSelected) {
[_quickformTableView deselectRowAtIndexPath:[_entryElement getIndexPath] animated:YES];
}
[control setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
- (BOOL)handleActionBarDone:(UIBarButtonItem *)doneButton {
[self endEditing:YES];
[self endEditing:NO];
[_textField resignFirstResponder];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
if(_entryElement && _entryElement.delegate && [_entryElement.delegate respondsToSelector:@selector(QEntryMustReturnForElement:andCell:)]){
[_entryElement.delegate QEntryMustReturnForElement:_entryElement andCell:self];
}
return NO;
}
- (BOOL)becomeFirstResponder {
[_textField becomeFirstResponder];
return YES;
}
- (BOOL)resignFirstResponder {
return YES;
}
- (void)applyAppearanceForElement:(QElement *)element {
[super applyAppearanceForElement:element];
QAppearance *appearance = element.appearance;
_textField.font = appearance.entryFont;
_textField.textColor = element.enabled ? appearance.entryTextColorEnabled : appearance.entryTextColorDisabled;
}
@end