mukeshpawar
mukeshpawar

Reputation: 125

TextField input limit in keyboard

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 or keyboard.

Upvotes: 0

Views: 781

Answers (2)

karim
karim

Reputation: 15589

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;
}

Upvotes: 0

Liam
Liam

Reputation: 8092

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

Upvotes: 2

Related Questions