Max length UITextField

后端 未结 18 1781
一个人的身影
一个人的身影 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:29

    I do it like this:

    func checkMaxLength(textField: UITextField!, maxLength: Int) {
        if (countElements(textField.text!) > maxLength) {
            textField.deleteBackward()
        }
    }
    

    The code works for me. But I work with storyboard. In Storyboard I add an action for the text field in the view controller on editing changed.

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

    Swift 5

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            let MAX_LENGTH = 4
            let updatedString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
            return updatedString.count <= MAX_LENGTH
        }
    
    0 讨论(0)
  • 2020-11-30 18:32

    In Swift 4

    10 Characters limit for text field and allow to delete(backspace)

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            if textField ==  userNameFTF{
                let char = string.cString(using: String.Encoding.utf8)
                let isBackSpace = strcmp(char, "\\b")
                if isBackSpace == -92 {
                    return true
                }
                return textField.text!.count <= 9
            }
            return true
        }
    
    0 讨论(0)
  • 2020-11-30 18:32

    Since delegates are a 1-to-1 relationship and I might want to use it elsewhere for other reasons, I like to restrict textfield length adding this code within their setup:

        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
            setup()
        }
    
        required override init(frame: CGRect) {
            super.init(frame: frame)
            setup()
        }
    
        func setup() {
    
            // your setup...
    
            setMaxLength()
        }
    
        let maxLength = 10
    
        private func setMaxLength() {
                addTarget(self, action: #selector(textfieldChanged(_:)), for: UIControlEvents.editingChanged)
            }
    
            @objc private func textfieldChanged(_ textField: UITextField) {
                guard let text = text else { return }
                let trimmed = text.characters.prefix(maxLength)
                self.text = String(trimmed)
    
            }
    
    0 讨论(0)
  • 2020-11-30 18:33

    You need to check whether the existing string plus the input is greater than 10.

       func textField(textField: UITextField!,shouldChangeCharactersInRange range: NSRange,    replacementString string: String!) -> Bool {
          NSUInteger newLength = textField.text.length + string.length - range.length;
          return !(newLength > 10)
       }
    
    0 讨论(0)
  • 2020-11-30 18:34

    Update for Swift 4

     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
         guard let text = textField.text else { return true }
         let newLength = text.count + string.count - range.length
         return newLength <= 10
    }
    
    0 讨论(0)
提交回复
热议问题