UITextField
設(shè)置密碼輸入時(shí)御蒲,需要設(shè)置isSecureTextEntry
(Swift
)屬性;但是如果明暗文切換诊赊,再次切換到暗文厚满,即密碼輸入,若此時(shí)再輸入碧磅,會(huì)使原來(lái)的輸入清空碘箍;在網(wǎng)上找到解決辦法如下:摘自:https://stackoverflow.com/questions/7305538/uitextfield-with-secure-entry-always-getting-cleared-before-editing
extension PasswordInputView: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let updateString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
inputTF.text = updateString
let selectedRange = NSMakeRange(range.location + string.count, 0)
let from = textField.position(from: textField.beginningOfDocument, offset:selectedRange.location)
let to = textField.position(from: from!, offset:selectedRange.length)
textField.selectedTextRange = textField.textRange(from: from!, to: to!)
textField.sendActions(for: UIControl.Event.editingChanged)
return false
}
}