UITextField - Allow only numbers and punctuation input/keypad

前端 未结 4 818
不思量自难忘°
不思量自难忘° 2020-12-30 10:16

I have tried the code below but that only allows for numbers on the keypad to be inputted. My app requires the keypad to use a period/full stop (for money transactions). The

4条回答
  •  無奈伤痛
    2020-12-30 10:37

    In Swift 3:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let allowedCharacters = "0123456789!@#$%^&*()_+~:{}|\"?><\\`,./;'[]=-"
        return allowedCharacters.contains(string) || range.length == 1
    }
    

提交回复
热议问题