Max length UITextField

后端 未结 18 1779
一个人的身影
一个人的身影 2020-11-30 17:42

When I\'ve tried How to you set the maximum number of characters that can be entered into a UITextField using swift?, I saw that if I use all 10 characters, I can\'t erase t

相关标签:
18条回答
  • 2020-11-30 18:13

    I posted a solution using IBInspectable, so you can change the max length value both in interface builder or programmatically. Check it out here

    0 讨论(0)
  • 2020-11-30 18:14

    You can use in swift 5 or swift 4 like image look like bellow

    1. Add textField in View Controller
    2. Connect to text to ViewController
    3. add the code in view ViewController

       class ViewController: UIViewController , UITextFieldDelegate {
      
        @IBOutlet weak var txtName: UITextField!
      
        var maxLen:Int = 8;
      
       override func viewDidLoad() {
          super.viewDidLoad()
      
          txtName.delegate = self
         }
      
       func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
      
           if(textField == txtName){
              let currentText = textField.text! + string
              return currentText.count <= maxLen
           }
      
           return true;
         }
      }
      

    You can download Full Source form GitHub: https://github.com/enamul95/TextFieldMaxLen

    0 讨论(0)
  • 2020-11-30 18:15

    Add More detail from @Martin answer

    // linked your button here
    @IBAction func mobileTFChanged(sender: AnyObject) {
        checkMaxLength(sender as! UITextField, maxLength: 10)
    }
    
    // linked your button here
    @IBAction func citizenTFChanged(sender: AnyObject) {
        checkMaxLength(sender as! UITextField, maxLength: 13)
    }
    
    func checkMaxLength(textField: UITextField!, maxLength: Int) {
        // swift 1.0
        //if (count(textField.text!) > maxLength) {
        //    textField.deleteBackward()
        //}
        // swift 2.0
        if (textField.text!.characters.count > maxLength) {
            textField.deleteBackward()
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:16
    func checkMaxLength(textField: UITextField!, maxLength: Int) {
            if (textField.text!.characters.count > maxLength) {
                textField.deleteBackward()
            }
    }
    

    a small change for IOS 9

    0 讨论(0)
  • 2020-11-30 18:19

    Beware of the undo bug for UITextField mentioned in this post: Set the maximum character length of a UITextField

    here is how you fix it in swift

    if(range.length + range.location > count(textField.text)) {
            return false;
    }
    
    0 讨论(0)
  • 2020-11-30 18:22

    Swift 3

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
                let nsString = NSString(string: textField.text!)
                let newText = nsString.replacingCharacters(in: range, with: string)
                return  newText.characters.count <= limitCount
        }
    
    0 讨论(0)
提交回复
热议问题