Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the maximum character length of a UITextField in Swift

I know there are other topics on this, but I can't seem to find out how to implement it.

I'm trying to limit a UITextField to only five characters.

Preferably alphanumeric, -, ., and _.

I've seen this code:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,                        replacementString string: String) -> Bool {     let maxLength = 4     let currentString: NSString = textField.text     let newString: NSString =              currentString.stringByReplacingCharactersInRange(range, withString: string)     return newString.length <= maxLength } 

and

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {      let length = count(textField.text.utf16) + count(string.utf16) - range.length     return length <= 10 } 

How can I actually implement it? Which "textfield" should I swap out for my custom named UITextField?

like image 673
ishkur88 Avatar asked Jul 12 '15 00:07

ishkur88


People also ask

How do I limit the number of characters in UITextField or UITextView?

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).

What can be the maximum length of a text field?

TINYTEXT is a string data type that can store up to to 255 characters. TEXT is a string data type that can store up to 65,535 characters. TEXT is commonly used for brief articles. LONGTEXT is a string data type with a maximum length of 4,294,967,295 characters.

What is maximum character length?

The maximum length of a text message is 160 characters as long as you use standard 7 bit characters. Once you use a character that is not in the 7 bit encoding list, your maximum number of characters in a text message drops to 70.


1 Answers

  1. Your view controller should conform to UITextFieldDelegate, like below:

    class MyViewController: UIViewController, UITextFieldDelegate {  } 
  2. Set the delegate of your textfield: myTextField.delegate = self

  3. Implement the method in your view controller:

    textField(_:shouldChangeCharactersInRange:replacementString:) 

All together:

class MyViewController: UIViewController, UITextFieldDelegate  // Set delegate to class  @IBOutlet var mytextField: UITextField             //  textfield variable  override func viewDidLoad() {     super.viewDidLoad()     mytextField.delegate = self                  // set delegate }   func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,                        replacementString string: String) -> Bool {     let maxLength = 4     let currentString: NSString = textField.text     let newString: NSString =              currentString.stringByReplacingCharactersInRange(range, withString: string)     return newString.length <= maxLength } 

For Swift 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {     let maxLength = 1     let currentString: NSString = (textField.text ?? "") as NSString     let newString: NSString =         currentString.replacingCharacters(in: range, with: string) as NSString     return newString.length <= maxLength } 

Allowing only a specified set of characters to be entered into a given text field

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {     var result = true      if mytextField == numberField {         if count(string) > 0 {             let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789.-").invertedSet             let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil             result = replacementStringIsLegal         }     }     return result } 

How to program an iOS text field that takes only numeric input with a maximum length

like image 168
Alaeddine Avatar answered Sep 21 '22 16:09

Alaeddine