0

How would I create a textfield for entering currency amount that would persist the “$” symbol? Essentially making it non deletable/interactable as a string?

Also as soon as I tap the textfield to enter an amount, I shouldn't have to manually delete the 0s. They should automatically clear unless there is an actual amount

Current Code:

struct CurrencyField: View {
    @State private var amount = 0.00
    
    private let numberFormatter: NumberFormatter
    
    init() {
        numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .currency
        numberFormatter.maximumFractionDigits = 2
    }
    
    var body: some View {
        TextField("$0.00", value: $amount, formatter: numberFormatter)
            .font(.system(size: 45, weight: .bold))
            .foregroundColor(.primary)
            .padding(.vertical, 1)
            .keyboardType(.decimalPad)
            .multilineTextAlignment(.center)
    }
}

enter image description here

WY34
  • 261
  • 3
  • 10

0 Answers0