Consumer enter proper to left, iOS App Improvement, SwiftUI, TextFields, Foreign money


General Context
I start with the under View, which supplies me the outcome picture of “Complete 0.00” that follows.

My drawback: Once I enter in values, it both provides the numbers originally or the tip of the "0.00" placeholder String in TextField. I can spotlight and overwrite the placeholder textual content of the TextField within the preview with keyboard enter.

My aim: I need the enter sequence to run as such:

  1. Consumer inputs the primary digit of their buy quantity
  2. This primary digit replaces the 0 within the 2nd decimal place of the placeholder textual content in TextField.
  3. Because the consumer enter the remaining digits of their purchaser worth, the numbers transfer proper to left.

Instance: For example the consumer’s buy worth was 29.50:

  1. Consumer inputs 2
  2. The TextField modifications from 0.00 to 0.02
  3. Consumer inputs 9, the TextField then reads 0.29

This continues till the consumer finishes inputing 29.50.

TLDR I need the enter to run proper to left, retaining the decimal within the applicable place.

import SwiftUI

struct ContentView: View {
    
  @State non-public var whole: String = "0.00"

  var physique: some View {
    NavigationView {
      Kind {
        Part(header: Textual content("Element")) {
          HStack {
            Textual content("Complete")
            TextField("0.00", textual content: $whole)
              .keyboardType(.decimalPad)
              .foregroundColor(.grey)
              .body(width: 120)
          }
        }
      }
    }
  }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Results of the above ContentView

Try to make use of .onEditingChanged

I changed the TextField with the under. Making an attempt the under code returns the next error on line the road with .onEditingChanged “Worth of sort ‘some View’ has no member ‘onEditingChanged'”

I realized .onEditingChanged isn’t a property of TextField so I wanted to strive one other method….

TextField("0.00", textual content: $whole)
    .keyboardType(.numberPad)
    .foregroundColor(.grey)
    .body(width: 120)
    .onEditingChanged { worth in
        let formatter = NumberFormatter()
        formatter.numberStyle = .foreign money
        formatter.locale = Locale(identifier: "en_US")
        if let outcome = formatter.string(from: NSNumber(worth: Double(worth) ?? 0)) {
            self.whole = outcome
        }
    }

Try to make use of .onCommit {}

I changed the .onEditingChanged { worth in from that try with .onCommit {. This resulted within the identical error message. It learn “Worth of sort ‘some View’ has no member ‘onCommit'”

TextField("0.00", textual content: $whole)
  .keyboardType(.decimalPad)
  .foregroundColor(.grey)
  .body(width: 120)
  .onCommit {
    let formatter = NumberFormatter()
    formatter.numberStyle = .foreign money
    formatter.locale = Locale(identifier: "en_US")
    if let outcome = formatter.string(from: NSNumber(worth: Double(self.whole) ?? 0)) {
      self.whole = outcome
    }
  }

I’m at a loss as the best way to obtain my aim. Thanks for the assistance upfront!

Leave a Reply