我有一个UITextView,其初始价格为0.00美元。用户可以通过输入数字来设置价格,当他们这样做时,从右边开始的每个字符将被键入的数字替换。例如:
你能告诉我如何在Swift中设置UITextField的那种输入吗?谢谢!
答案 0 :(得分:1)
使用UITextFieldDelete并实现以下内容:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text: NSString = (textField.text ?? "") as NSString
let resultString = text.replacingCharacters(in: range, with: string)
// Take the result string (user's text input) and format it how you wish...
let formattedText = ...
// Set the newly formatted string to the text fields's text property...
textField.text = formattedText
// Return false so the user's input is not applied to the text field (you're doing it on their behalf).
return false
}