Wednesday, May 31, 2023
HomeiOS DevelopmentPerson enter proper to left, iOS App Improvement, SwiftUI, TextFields, Foreign money

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


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

My downside: After 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 objective: I need the enter sequence to run as such:

  1. Person 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 worth, the numbers transfer proper to left.

Instance: To illustrate the person’s buy worth was 29.50:

  1. Person inputs 2
  2. The TextField adjustments from 0.00 to 0.02
  3. Person 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, maintaining the decimal within the acceptable place.

import SwiftUI

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

  var physique: some View {
    NavigationView {
      Kind {
        Part(header: Textual content("Element")) {
          HStack {
            Textual content("Whole")
            TextField("0.00", textual content: $complete)
              .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 isn’t a property of TextField so I wanted to attempt one other method….

TextField("0.00", textual content: $complete)
    .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 end result = formatter.string(from: NSNumber(worth: Double(worth) ?? 0)) {
            self.complete = end result
        }
    }

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 kind ‘some View’ has no member ‘onCommit'”

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments