【发布时间】:2018-07-11 14:48:17
【问题描述】:
(在 swift 2 中编程)
我有一个 UITextField,当用户输入它时,它应该在输入时自动转换为小写(所以不是在表单验证之后)。
我已经走到这一步了:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//only convert to lowercase for username field
if textField == textFieldUsername {
//let stringRange = NSRange(location: range.location, length: range.length)
let completedString = (textField.text ?? "" as NSString).stringByReplacingCharactersInRange(range, withString: string)
//convert the while thing to lowercase and assign back to the textfield
textField.text = completedString.lowercaseString
//return false to indicate that the "system" itself should not do anychanges anymore, as we did them
return false
}
//return to the "system" that it can do the changes itself
return true
}
问题在于 (1) 当用户按住 UITextField 以 (2) 将光标移动到字符串中间的某个位置并 (3) 开始输入 (4) 光标跳回已经输入了字符串。
textField: shouldChangeCharactersInRange 调用后是否需要恢复光标位置?
【问题讨论】:
-
不行,光标有问题....
-
这不是游标问题。这可以改进。参考:[链接]stackoverflow.com/questions/4180263/…