我有 textfield
,shouldChangeCharactersIn
代码我使用条形码,系统和我的条形码代码 == 12 个字符。如果条形码小 12 个字符或大必须返回 false 或干净的文本字段,我只想在我的字符串中显示 12 个字符。我的代码在下面。当我使用条形码时,如果我的条形码 12 个字符工作正常,但如果我读取 12 个字符低,请不要清除文本字段,
How can I fix it ?
我的代码在下面。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let length = (Mytext.text?.characters.count)! - range.length + string.characters.count
if length == 13 {
print("Mytext=\(Mytext.text!)")
Mytext.text = ""
return true
}else {
return true
}
}
读取12个字符条码时输出显示成功! 当阅读低 12 个字符时不要清除 Mytext 字段,当我再次阅读时添加 +
最佳答案
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (string != "" && textField.text?.count == 12) {
print("Mytext = \(textField.text!)")
textField.text = ""
return false
}
return true
}
关于ios - Swift 4 shouldChangeCharactersIn 只显示长度 = 12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642393/