This subclass of the UITextField, adds a text length limit.
- Set text length limit
- Text length counter
- IME support(japanese keyboard support)
UITextFieldWithLimit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "UITextFieldWithLimit"
You can try this : pod try UITextFieldWithLimit
- (void)viewDidLoad {
[super viewDidLoad];
self.limitedTextField = [[UITextFieldWithLimit alloc] init];
self.limitedTextField.maxLength = @15;
// optional
self.limitedTextField.limitDelegate = self;
}
- (void)textFieldLimit:(UITextFieldWithLimit *) textFieldLimit didReachLimitWithLastEnteredText:(NSString *) text inRange:(NSRange) range {
NSLog(@"%s", sel_getName(_cmd));
}
- (void)textFieldLimit:(UITextFieldWithLimit *) textFieldLimit didWentOverLimitWithDisallowedText:(NSString *) text inDisallowedRange:(NSRange) range {
NSLog(@"%s", sel_getName(_cmd));
}
@protocol UITextFieldWithLimitDelegate <UITextFieldDelegate>
@optional
- (void)textFieldLimit:(UITextFieldWithLimit *) textFieldLimit didWentOverLimitWithDisallowedText:(NSString *) text inDisallowedRange:(NSRange) range;
- (void)textFieldLimit:(UITextFieldWithLimit *) textFieldLimit didReachLimitWithLastEnteredText:(NSString *) text inRange:(NSRange) range;
@end
@interface UITextFieldWithLimit : UITextField <UITextFieldDelegate>
@property(nonatomic, strong) id <UITextFieldWithLimitDelegate> limitDelegate;
// Default : @10
@property(readwrite, nonatomic) NSNumber *maxLength;
@property(strong, nonatomic) UILabel *limitLabel;
@end
azu, [email protected]
UITextFieldWithLimit is available under the MIT license. See the LICENSE file for more info.