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
//
// IQKeyboardReturnKeyHandler.m
// https://github.com/hackiftekhar/IQKeyboardManager
// Copyright (c) 2013-15 Iftekhar Qurashi.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "IQKeyboardReturnKeyHandler.h"
#import "IQUIView+Hierarchy.h"
#import "IQNSArray+Sort.h"
#import <Foundation/NSSet.h>
#import <UIKit/UITextField.h>
#import <UIKit/UITextView.h>
#import <UIKit/UITableView.h>
#import <UIKit/UIViewController.h>
#ifdef NSFoundationVersionNumber_iOS_5_1
#import <UIKit/UICollectionView.h>
#endif
NSString *const kIQTextField = @"kIQTextField";
NSString *const kIQTextFieldDelegate = @"kIQTextFieldDelegate";
NSString *const kIQTextFieldReturnKeyType = @"kIQTextFieldReturnKeyType";
@interface IQKeyboardReturnKeyHandler ()<UITextFieldDelegate,UITextViewDelegate>
-(void)updateReturnKeyTypeOnTextField:(UIView*)textField;
@end
@implementation IQKeyboardReturnKeyHandler
{
NSMutableSet *textFieldInfoCache;
}
@synthesize lastTextFieldReturnKeyType = _lastTextFieldReturnKeyType;
@synthesize toolbarManageBehaviour = _toolbarManageBehaviour;
@synthesize delegate = _delegate;
-(instancetype)initWithViewController:(UIViewController*)controller
{
self = [super init];
if (self)
{
textFieldInfoCache = [[NSMutableSet alloc] init];
[self addResponderFromView:controller.view];
}
return self;
}
-(NSDictionary*)textFieldCachedInfo:(UITextField*)textField
{
for (NSDictionary *infoDict in textFieldInfoCache)
if ([infoDict objectForKey:kIQTextField] == textField) return infoDict;
return nil;
}
#pragma mark - Add/Remove TextFields
-(void)addResponderFromView:(UIView*)view
{
NSArray *textFields = [view deepResponderViews];
for (UITextField *textField in textFields) [self addTextFieldView:textField];
}
-(void)removeResponderFromView:(UIView*)view
{
NSArray *textFields = [view deepResponderViews];
for (UITextField *textField in textFields) [self removeTextFieldView:textField];
}
-(void)removeTextFieldView:(UITextField*)textField
{
NSDictionary *dict = [self textFieldCachedInfo:textField];
if (dict)
{
textField.keyboardType = [[dict objectForKey:kIQTextFieldReturnKeyType] integerValue];
textField.delegate = [dict objectForKey:kIQTextFieldDelegate];
[textFieldInfoCache removeObject:dict];
}
}
-(void)addTextFieldView:(UITextField*)textField
{
NSMutableDictionary *dictInfo = [[NSMutableDictionary alloc] init];
[dictInfo setObject:textField forKey:kIQTextField];
[dictInfo setObject:[NSNumber numberWithInteger:textField.returnKeyType] forKey:kIQTextFieldReturnKeyType];
if (textField.delegate) [dictInfo setObject:textField.delegate forKey:kIQTextFieldDelegate];
[textField setDelegate:self];
[textFieldInfoCache addObject:dictInfo];
}
#pragma mark - Overriding lastTextFieldReturnKeyType
-(void)setLastTextFieldReturnKeyType:(UIReturnKeyType)lastTextFieldReturnKeyType
{
_lastTextFieldReturnKeyType = lastTextFieldReturnKeyType;
for (NSDictionary *infoDict in textFieldInfoCache)
{
UITextField *textField = [infoDict objectForKey:kIQTextField];
[self updateReturnKeyTypeOnTextField:textField];
}
}
-(void)updateReturnKeyTypeOnTextField:(UIView*)textField
{
UIView *tableView = [textField superviewOfClassType:[UITableView class]];
#ifdef NSFoundationVersionNumber_iOS_5_1
if (tableView == nil) tableView = [textField superviewOfClassType:[UICollectionView class]];
#endif
NSArray *textFields = nil;
//If there is a tableView in view's hierarchy, then fetching all it's subview that responds. No sorting for tableView, it's by subView position.
if (tableView) // // (Enhancement ID: #22)
{
textFields = [tableView deepResponderViews];
}
//Otherwise fetching all the siblings
else
{
textFields = [textField responderSiblings];
//Sorting textFields according to behaviour
switch (_toolbarManageBehaviour)
{
//If needs to sort it by tag
case IQAutoToolbarByTag:
textFields = [textFields sortedArrayByTag];
break;
//If needs to sort it by Position
case IQAutoToolbarByPosition:
textFields = [textFields sortedArrayByPosition];
break;
default:
break;
}
}
//If it's the last textField in responder view, else next
[(UITextField*)textField setReturnKeyType:(([textFields lastObject] == textField) ? self.lastTextFieldReturnKeyType : UIReturnKeyNext)];
}
#pragma mark - Goto next or Resign.
-(void)goToNextResponderOrResign:(UIView*)textField
{
UIView *tableView = [textField superviewOfClassType:[UITableView class]];
#ifdef NSFoundationVersionNumber_iOS_5_1
if (tableView == nil) tableView = [textField superviewOfClassType:[UICollectionView class]];
#endif
NSArray *textFields = nil;
//If there is a tableView in view's hierarchy, then fetching all it's subview that responds. No sorting for tableView, it's by subView position.
if (tableView) // // (Enhancement ID: #22)
{
textFields = [tableView deepResponderViews];
}
//Otherwise fetching all the siblings
else
{
textFields = [textField responderSiblings];
//Sorting textFields according to behaviour
switch (_toolbarManageBehaviour)
{
//If needs to sort it by tag
case IQAutoToolbarByTag:
textFields = [textFields sortedArrayByTag];
break;
//If needs to sort it by Position
case IQAutoToolbarByPosition:
textFields = [textFields sortedArrayByPosition];
break;
default:
break;
}
}
if ([textFields containsObject:textField])
{
//Getting index of current textField.
NSUInteger index = [textFields indexOfObject:textField];
//If it is not last textField. then it's next object becomeFirstResponder.
(index < textFields.count-1) ? [[textFields objectAtIndex:index+1] becomeFirstResponder] : [textField resignFirstResponder];
}
}
#pragma mark - TextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
return [self.delegate textFieldShouldBeginEditing:textField];
else
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self updateReturnKeyTypeOnTextField:textField];
if ([self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
[self.delegate textFieldDidBeginEditing:textField];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
return [self.delegate textFieldShouldEndEditing:textField];
else
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)])
[self.delegate textFieldDidEndEditing:textField];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)])
return [self.delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
else
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(textFieldShouldClear:)])
return [self.delegate textFieldShouldClear:textField];
else
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
BOOL shouldReturn = YES;
if ([self.delegate respondsToSelector:@selector(textFieldShouldReturn:)])
shouldReturn = [self.delegate textFieldShouldReturn:textField];
if (shouldReturn)
{
[self goToNextResponderOrResign:textField];
}
return shouldReturn;
}
#pragma mark - TextView delegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if ([self.delegate respondsToSelector:@selector(textViewShouldBeginEditing:)])
return [self.delegate textViewShouldBeginEditing:textView];
else
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
if ([self.delegate respondsToSelector:@selector(textViewShouldEndEditing:)])
return [self.delegate textViewShouldEndEditing:textView];
else
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self updateReturnKeyTypeOnTextField:textView];
if ([self.delegate respondsToSelector:@selector(textViewDidBeginEditing:)])
[self.delegate textViewDidBeginEditing:textView];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([self.delegate respondsToSelector:@selector(textViewDidEndEditing:)])
[self.delegate textViewDidEndEditing:textView];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
BOOL shouldReturn = YES;
if ([self.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
shouldReturn = [self.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
if (shouldReturn && [text isEqualToString:@"\n"])
{
[self goToNextResponderOrResign:textView];
}
return shouldReturn;
}
- (void)textViewDidChange:(UITextView *)textView
{
if ([self.delegate respondsToSelector:@selector(textViewDidChange:)])
[self.delegate textViewDidChange:textView];
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
if ([self.delegate respondsToSelector:@selector(textViewDidChangeSelection:)])
[self.delegate textViewDidChangeSelection:textView];
}
#ifdef NSFoundationVersionNumber_iOS_6_1
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([self.delegate respondsToSelector:@selector(textView:shouldInteractWithURL:inRange:)])
return [self.delegate textView:textView shouldInteractWithURL:URL inRange:characterRange];
else
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
if ([self.delegate respondsToSelector:@selector(textView:shouldInteractWithTextAttachment:inRange:)])
return [self.delegate textView:textView shouldInteractWithTextAttachment:textAttachment inRange:characterRange];
else
return YES;
}
#endif
-(void)dealloc
{
for (NSDictionary *dict in textFieldInfoCache)
{
UITextField *textField = [dict objectForKey:kIQTextField];
textField.keyboardType = [[dict objectForKey:kIQTextFieldReturnKeyType] integerValue];
textField.delegate = [dict objectForKey:kIQTextFieldDelegate];
}
[textFieldInfoCache removeAllObjects];
}
@end