Restrict UITextField size
cocoa-touch, objective-c, uitextfield
Solution
Here is a code sample to limit the size of a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([[textField text] length] + [string length] - range.length > MAX_LENGTH) {
return NO;
} else {
return YES;
}
}
Problem
How can I limit the number of characters in a UITextField? I.e if I limit it to 10 characters, the user will be unable to enter more than 10. Also I want to prevent him from entering some special characters like +, =, etc. How would I do this?