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.

SwiftUser
  • 555
  • 6
  • 17
  • Did you try creating a manual binding using something like TextField("0", value: Binding {formattedPrice} set: {newString in formatPrice(newString)})? – Damiaan Dufaux Jul 10 '21 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](https://stackoverflow.com/questions/56799456/swiftui-textfield-with-formatter-not-working) on how to workaround this – lorem ipsum Jul 10 '21 at 21:24
  • Does this answer your question? [SwiftUI TextField with formatter not working?](https://stackoverflow.com/questions/56799456/swiftui-textfield-with-formatter-not-working) – lorem ipsum Jul 10 '21 at 21:24
  • see the answer at: https://stackoverflow.com/questions/67888195/i-want-to-add-sign-to-textfield-in-swiftui/67895810#67895810 – workingdog support Ukraine Jul 10 '21 at 22:45
  • @loremipsum You answer was the closest to any luck, however, the issue is still not resolved. – SwiftUser Jul 11 '21 at 12:35

0 Answers0