Swift 限制UITextField的输入长度
【代码】Swift 限制UITextField的输入长度。
·
let maxLength = 30
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= maxLength
}
更多推荐
已为社区贡献1条内容
所有评论(0)