Blane Townsend
Blane Townsend

Reputation: 3038

How to put a character limit on a UITextField

I would like to put a character limit on a UITextField but don't know how. I want it to have a maximum of 16 characters in it. How do I do this.

Upvotes: 4

Views: 5110

Answers (1)

DenverCoder9
DenverCoder9

Reputation: 3705

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 12) ? NO : YES;
}

Upvotes: 16

Related Questions