📌  相关文章
📜  如果文本字段为空,则在编辑输入字段时提供默认值 swift 代码示例

📅  最后修改于: 2022-03-11 15:00:57.654000             🧑  作者: Mango

代码示例1
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let textFieldString = textField.text, let range = Range(range, in: textFieldString) else {
        return false
    }
    let newString = textFieldString.replacingCharacters(in: range, with: string)
    if newString.isEmpty {
        textField.text = "0"
        return false
    } else if textField.text == "0" {
        textField.text = string
        return false
    }
    return true
}