Tag Archives: UITextFieldDelegate

iPhone – Set Maximum Length of String in UITextField

There is no maximum length attribute in the UITextField. Here is a work around to limit the number of input characters in an UITextField.

1. Implement the UITextFieldDelegate in the .h header file

@interface HelloViewController : UIViewController <UITextFieldDelegate> {


2. Implement this function in the .m implementation file to set the maximum length to 4 characters

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


Done =)

Reference: iPhone SDK: Set Max Character length TextField