currency formatter is the way to go, but if you just want to show a $ in the TextField as you type, you could use something like this:
(you can of course combine this approach with a Formatter)
struct ContentView: View {
@State var price = ""
var body: some View {
VStack {
ZStack(alignment: .leading) {
if price.isEmpty {
Text("Enter total budget")
}
HStack {
TextField("", text: Binding(
get: { price },
set: { newVal in
if price.starts(with: "$") {
price = newVal
} else {
price = "$" + newVal
}
})).keyboardType(.decimalPad)
}
}.padding(20)
Text("number entered: " + String(price.dropFirst()))
}
}
}