Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UITextField Maximum Length [duplicate]

Is there any way to set the maximum length on a UITextField?

Something like the MAXLENGTH attribute in HTML input fields.

like image 860
Elegya Avatar asked Mar 26 '10 13:03

Elegya


People also ask

How do I limit the number of characters in UITextField?

If you have a UITextField or UITextView and want to stop users typing in more than a certain number of letters, you need to set yourself as the delegate for the control then implement either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).

How do I limit text length in Swift?

Create textLimit method This is the first step in limiting the characters for a text field or a text view. We need to create a method that will limit the the number of characters based on the current character count and the additional/new text that will get appended to the current value of the text field or text view.

What can be the maximum length of a text field?

The maxlength attribute specifies the maximum number of characters that can be entered. By default, the maximum is 524,288 characters.

How do I count the number of characters in a string in Swift?

Swift – String Length/Count To get the length of a String in Swift, use count property of the string. count property is an integer value representing the number of characters in this string.


2 Answers

This works correctly with backspace and copy & paste:

#define MAXLENGTH 10  - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {      NSUInteger oldLength = [textField.text length];     NSUInteger replacementLength = [string length];     NSUInteger rangeLength = range.length;      NSUInteger newLength = oldLength - rangeLength + replacementLength;      BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;      return newLength <= MAXLENGTH || returnKey; } 

UPDATE: Updated to accept the return key even when at MAXLENGTH. Thanks Mr Rogers!

like image 114
Tomas Andrle Avatar answered Sep 23 '22 17:09

Tomas Andrle


UPDATE

I cannot delete this answer because it is the accepted one, but it was not correct. Here is the correct code, copied from TomA below:

#define MAXLENGTH 10  - (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {      NSUInteger oldLength = [textField.text length];     NSUInteger replacementLength = [string length];     NSUInteger rangeLength = range.length;      NSUInteger newLength = oldLength - rangeLength + replacementLength;      BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;      return newLength <= MAXLENGTH || returnKey; } 

ORIGINAL

I think you mean UITextField. If yes, then there is a simple way.

  1. Implement the UITextFieldDelegate protocol
  2. Implement the textField:shouldChangeCharactersInRange:replacementString: method.

That method gets called on every character tap or previous character replacement. in this method, you can do something like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {     if ([textField.text length] > MAXLENGTH) {         textField.text = [textField.text substringToIndex:MAXLENGTH-1];         return NO;     }     return YES; } 
like image 38
coneybeare Avatar answered Sep 20 '22 17:09

coneybeare