Swift 4 shouldChangeCharactersIn 只显示长度 = 12
Swift 4 shouldChangeCharactersIn only show length = 12
我有 textfield
,shouldChangeCharactersIn
代码,我使用条形码、系统和我的条形码代码 == 12 个字符。如果 barcode small 12 characters or big must be return false or clean text field ,我只想在我的字符串中显示 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 {
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
}
}
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
}
我有 textfield
,shouldChangeCharactersIn
代码,我使用条形码、系统和我的条形码代码 == 12 个字符。如果 barcode small 12 characters or big must be return false or clean text field ,我只想在我的字符串中显示 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 {
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
}
}
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
}