当我长按键盘上的退格键时,有时会清除所有文本,有没有办法只清除一个字符
发布于 2020-02-27 17:32:29
您可以使用textField shouldChangeCharactersIn委托方法进行处理,如下所示:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.isEmpty {
if range.length != 1 {
if let val: String = textField.text {
if !val.isEmpty {
let newStr: NSString = val as NSString
if newStr.length > 0 {
let updatedString: String = newStr.substring(to: newStr.length - 1)
textField.text = updatedString
}
}
}
return false
}
}
return true
}发布于 2020-02-27 18:32:56
像这样实现UITextFieldDelegate's textField(_:shouldChangeCharactersIn:replacementString:)方法,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.isEmpty {
textField.text?.removeLast()
return false
}
return true
}https://stackoverflow.com/questions/60429985
复制相似问题