Set Texfield maximum limitation of characters from the user input in iOS Swift 3


You must have to use a text field delegate method.  add text field delegate in the class and use the below method and also connect the delegate to the view controller in the storyboard.The below method will control the user input if they use the keypad as the number then it will be maximum 10 characters allowed.Otherwise only 20 characters allowed.



func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let text = textField.text else { return true }
        let newLength = text.utf16.count + string.utf16.count - range.length
        if textField.keyboardType == .numberPad
        {
            return newLength <= 10
        } else {
            return newLength <= 20
        }
        // Bool




    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift