(編程在迅速2)強制的UITextField爲小寫打字時和保持光標位置
我有一個的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被調用可能嗎?
也許這篇文章可以幫助http://stackoverflow.com/questions/33195946/force-lowercase-ios-swift – bhmahler
NOP,這與光標的問題.... – HixField
這不是遊標問題。這可以改進。參考: [鏈接] http://stackoverflow.com/questions/4180263/moving-the-cursor-to-the-beginning-of-uitextfield – UIResponder