How can I limit the number of decimal points in a UITextField?

后端 未结 15 2286
说谎
说谎 2020-12-01 05:20

I have a UITextField that when clicked brings up a number pad with a decimal point in the bottom left. I am trying to limit the field so that a user can only place 1 decimal

15条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 06:00

    Swift 3

    No need to create an array and check count. Limit user can only place 1 decimal mark like this.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if (textField.text?.contains("."))! && string.contains(".")
        {
            return false
        }
        else
        {
            return true
        }
    }
    

提交回复
热议问题