开发者

TextField input limit in keyboard

开发者 https://www.devze.com 2022-12-29 09:56 出处:网络
Can we define limit of inout in text field i wan that after certain number of character the keyboard should get hide. i should code on which event of textfields o开发者_开发知识库r keyboard.What you c

Can we define limit of inout in text field i wan that after certain number of character the keyboard should get hide. i should code on which event of textfields o开发者_开发知识库r keyboard.


What you can do is catch an event 'Editing Changed' on the text field in IB and get it to call an IBAction in your controller e.g. verifyInputLimit.

In this method you call resignFirstResponder when the size you require is reached

if ([myLimitedTextField.text length]>=MY_LIMIT {
   [myLimitedTextField.text resignFirstResponder];
}

The user would still be able to 'paste' some text into this field which is greater than your limit. If you don't want this then you can truncate it in the same method


Override the following method of UITextFieldDelegate,

- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string
{
    if (aTextField == tfAccNumber) {
        if (aTextField.text.length >= MAX_LENGTH && range.length == 0) {
            return FALSE;
        }
    }   
    return TRUE;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消