iOS 文本框光标位置错误
iOS textfield cursor position wrong
切换 iOS textField isSecureTenxtEntry。
我的代码是这样的
textField?.isSecureTextEntry = !textField.isSecureTextEntry
设备是iPhone6.
This problem raised due to the different character size of the secure dot character
and simple character. Dot character are wider in size that's why when you disable secure text entry, character contracts but cursor stays at the same position.
虽然这个问题应该不是最新的 Xcode 但是我在 Xcode 6.3 中遇到了同样的问题。
解释:
当您单击按钮切换 secureTextEntry
时,再次为您的 textField 设置 becomeFirstResponder
,它将调用键盘观察器并将光标重置到正确的位置。
textField?.isSecureTextEntry = !textField.isSecureTextEntry
if textField?.isFirstResponder {
textField?.becomeFirstResponder()
}
希望对您有所帮助。
首先讨论为什么会这样。
点字符宽度不同于普通字符,因此当您更改 isSecureTextEntry
属性 的值时,UITextField
不会刷新,这就是为什么会出现额外的 space。
要解决这个问题,您可以使用下面的代码
txtPassword.isSecureTextEntry = !txtPassword.isSecureTextEntry
let str = txtPassword.text
txtPassword.text = ""
txtPassword.text = str
我在上面的代码中所做的是,在更改 isSecureTextEntry
的值后,将值存储在临时变量中并清空 UITextField
,然后使用临时变量重新分配相同的 UITextField
。
切换 iOS textField isSecureTenxtEntry。
我的代码是这样的
textField?.isSecureTextEntry = !textField.isSecureTextEntry
设备是iPhone6.
This problem raised due to the different character size of the
secure dot character
and simple character. Dot character are wider in size that's why when you disable secure text entry, character contracts but cursor stays at the same position.
虽然这个问题应该不是最新的 Xcode 但是我在 Xcode 6.3 中遇到了同样的问题。
解释:
当您单击按钮切换 secureTextEntry
时,再次为您的 textField 设置 becomeFirstResponder
,它将调用键盘观察器并将光标重置到正确的位置。
textField?.isSecureTextEntry = !textField.isSecureTextEntry
if textField?.isFirstResponder {
textField?.becomeFirstResponder()
}
希望对您有所帮助。
首先讨论为什么会这样。
点字符宽度不同于普通字符,因此当您更改 isSecureTextEntry
属性 的值时,UITextField
不会刷新,这就是为什么会出现额外的 space。
要解决这个问题,您可以使用下面的代码
txtPassword.isSecureTextEntry = !txtPassword.isSecureTextEntry
let str = txtPassword.text
txtPassword.text = ""
txtPassword.text = str
我在上面的代码中所做的是,在更改 isSecureTextEntry
的值后,将值存储在临时变量中并清空 UITextField
,然后使用临时变量重新分配相同的 UITextField
。