Answers for "swift uitextfield only numbers keyboard lock programmatically"

0

swift uitextfield only numbers keyboard lock programmatically

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // you can set the following two properties for the text field in Interface Builder, if you'd prefer

        textField.delegate = self
        textField.keyboardType = .numberPad
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let invalidCharacters = CharacterSet(charactersIn: "0123456789").inverted
        return string.rangeOfCharacter(from: invalidCharacters) == nil
    }

    // or, alternatively:
    //
    // func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    //     return string.range(of: "^\\d*$", options: .regularExpression) != nil
    // }

}
Posted by: Guest on October-08-2020

Code answers related to "swift uitextfield only numbers keyboard lock programmatically"

Code answers related to "Swift"

Browse Popular Code Answers by Language