Consumer enter proper to left, iOS App Improvement, SwiftUI, TextFields, Forex


Total Context
I start with the under View, which supplies me the end result picture of “Whole 0.00” that follows.

My drawback: After I enter in values, it both provides the numbers at the start or the top 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 person enter the remaining digits of their purchaser value, the numbers transfer proper to left.

Instance: For example the person’s buy value was 29.50:

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

This continues till the person finishes inputing 29.50.

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

import SwiftUI

struct ContentView: View {
    
  @State personal var whole: String = "0.00"

  var physique: some View {
    NavigationView {
      Kind {
        Part(header: Textual content("Element")) {
          HStack {
            Textual content("Whole")
            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 kind ‘some View’ has no member ‘onEditingChanged'”

I discovered .onEditingChanged is just not a property of TextField so I wanted to attempt one other method….

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

Try to make use of .onCommit {}

I changed the .onEditingChanged { worth in from that try with .onCommit {. This resulted within the similar error message. It learn “Worth of kind ‘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 = .forex
    formatter.locale = Locale(identifier: "en_US")
    if let end result = formatter.string(from: NSNumber(worth: Double(self.whole) ?? 0)) {
      self.whole = end result
    }
  }

I’m at a loss as how you can obtain my aim. Thanks for the assistance prematurely!

Leave a Reply