Last active
July 1, 2019 16:53
-
-
Save niw/931a64c0dd78bd805912 to your computer and use it in GitHub Desktop.
The right solution to limit length of a text field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Why not simply use textField:shouldChangeCharactersInRange:replacementString:? | |
=== | |
If you google how to limit a length of text field, you'll see many implementations simply using textField:shouldChangeCharactersInRange:replacementString: like | |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string | |
{ | |
return [[textField.text stringByReplacingCharactersInRange:range withString:string] length] <= MAX_LENGTH; | |
} | |
It looks work, however, it actually doesn't works with CJK and some other languages keyboards. | |
Actually textField:shouldChangeCharactersInRange:replacementString: should always returns YES, otherwise you'll see some troubles with these keyboards. | |
I think it's Apple's API design issue. | |
Because of this behavior, limiting a length of text field is not easy. | |
This is an example of better implementation which can handle many edge cases and works CJK keyboards. | |
--- | |
Copyright (c) 2014 Yoshimasa Niwa | |
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 <UIKit/UIKit.h> | |
@interface UITextFieldLimitLengthDelegate : NSObject <UITextFieldDelegate> | |
@property (nonatomic) NSUInteger maxLength; | |
/** | |
* Use this class as a delegate for the text field. | |
*/ | |
- (instancetype)initWithTextField:(UITextField *)textField maxLength:(NSUInteger)maxLength; | |
/** | |
* If you're not using this limiter as a delegate for the text field, you should call this method | |
* from your textField:shouldChangeCharactersInRange:replacementString: delegate method. | |
*/ | |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; | |
@end | |
@implementation UITextFieldLimitLengthDelegate | |
{ | |
NSString *_previousText; | |
NSRange _lastReplaceRange; | |
NSString *_lastReplacementString; | |
} | |
- (instancetype)init | |
{ | |
return [self initWithTextField:nil maxLength:0]; | |
} | |
- (instancetype)initWithTextField:(UITextField *)textField maxLength:(NSUInteger)maxLength | |
{ | |
if (self = [super init]) { | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; | |
_maxLength = maxLength; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string | |
{ | |
_previousText = textField.text; | |
_lastReplaceRange = range; | |
_lastReplacementString = string; | |
return YES; | |
} | |
- (void)_textFieldDidChange:(NSNotification *)notification | |
{ | |
UITextField *textField = (UITextField *)notification.object; | |
if (textField.markedTextRange) { | |
return; | |
} | |
if ([textField.text length] > _maxLength) { | |
NSInteger offset = _maxLength - [textField.text length]; | |
NSString *replacementString = [_lastReplacementString substringToIndex:([_lastReplacementString length] + offset)]; | |
NSString *text = [_previousText stringByReplacingCharactersInRange:_lastReplaceRange withString:replacementString]; | |
UITextPosition *position = [textField positionFromPosition:textField.selectedTextRange.start offset:offset]; | |
UITextRange *selectedTextRange = [textField textRangeFromPosition:position toPosition:position]; | |
textField.text = text; | |
textField.selectedTextRange = selectedTextRange; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment