【发布时间】:2017-02-04 02:20:08
【问题描述】:
我需要在三个文本字段中输入电话号码。我正在尝试将每个文本字段的字符限制设置为三个字符,一旦达到此限制,就切换到新的文本字段。
我在网上看到用这个代码来限制字符:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 25
}
并使用它来切换到在新文本字段上输入:
.didbecomefirstresponder()
但我不确定如何将文本字段限制为 3 个字符,然后切换到下一个字段。
【问题讨论】:
标签: swift string uitextfield