I want to add thousands of separators to a text field while typing a number. My idea was to react to a onChange event, format the entered number and then write the formatted text into the text field:
import Foundation
import SwiftUI
struct TestFormatTextFieldView: View {
@State private var stringAmount: String = ""
var body: some View {
NavigationStack {
Form {
TextField("Amount", text: $stringAmount)
.keyboardType(.numbersAndPunctuation)
.onChange(of: stringAmount, perform: { newValue in
stringAmount = formatDouble(input: newValue)
})
Button("Request price") {
self.requestPrice()
}
}
}
}
func formatDouble(input : String) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
// If the String can't be cast as a Double return ""
guard let resultAsDouble = Double(input) else {
return ""
}
let result = formatter.string(from: NSNumber(value: resultAsDouble)) ?? ""
print("formatted \(result)")
return result
}
func requestPrice() {
print("request \(stringAmount)")
}
}
struct TestFormatTextFieldView_Prev: PreviewProvider {
static var previews: some View {
ContentView()
}
}
If I write more than 4 digits, I get the following error: onChange(of: String) action tried to update multiple times per frame. The problem is that formatting changes the length of the text. Is it still possible to add thousands of separators on the fly?