1

So I'm using a TextField with a formatter, the issue is unless the user hits return that value is not captured which is causing some distress for users, even though I have added a label stating they should hit return, but in fairness it doesn't seem professional. How can I combat this issue? Is there a way in which I can capture the input as the user inputs data into the textfield, as well as formatting the input simultaneously?

This is my code:

  var currencyFormatter: NumberFormatter = {
        let val  = NumberFormatter()
        val.numberStyle = .currency
        val.currencyCode = "GBP"
        return val
    }()

   TextField("0", value: $viewModel.price, formatter: currencyFormatter)
   .foregroundColor(Color(SYSTEM_FONT_COLOUR))

Thank you.

5
  • Did you try creating a manual binding using something like TextField("0", value: Binding {formattedPrice} set: {newString in formatPrice(newString)})? Commented Jul 10, 2021 at 18:02
  • This will be remedied in iOS 15 I think they mention it in What is new in SwiftUI or maybe Demystify SwiftUI. There are plenty of SO questions on how to workaround this Commented Jul 10, 2021 at 21:24
  • Does this answer your question? SwiftUI TextField with formatter not working? Commented Jul 10, 2021 at 21:24
  • see the answer at: stackoverflow.com/questions/67888195/… Commented Jul 10, 2021 at 22:45
  • @loremipsum You answer was the closest to any luck, however, the issue is still not resolved. Commented Jul 11, 2021 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.