I'm running into a SwiftUI end-user, usability problem using a TextField to enter a currency amount.
The bound field is a double, initially set to 0 and when the text field is displayed, the prompt is $0.00.
The problem is that when the user wants to enter a value, they have to erase the 0.00 with the backspace key, manually. Also, if they accidentally backspace over the $-sign, any value entered thereafter disappears!
When there are multiple currency fields, this is a real nuisance for the end-user.
I've seen suggestions on the Internet to set the formatter.zeroSymbol = "" in the NumberFormatter, but when numberStyle = .currency any value entered is lost/destroyed.
If I change the number style to .decimal, I can use the zeroSymbol option and it seems to work, but I lose the currency formatting.
Does anyone know how to fix this?
Following is a sample code that you can run that demonstrates this problem.
import SwiftUI
struct ContentView: View {
@State private var amount: Double = 0
let currencyFormat: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
// formatter.zeroSymbol = ""
return formatter
}()
var body: some View {
HStack {
Text("Enter Amount")
Spacer()
TextField("", value: $amount, formatter: currencyFormat)
.keyboardType(.numbersAndPunctuation)
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Instead of this one:
Use this one: