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:
- Person inputs the primary digit of their buy quantity
- This primary digit replaces the
0
within the 2nd decimal place of the placeholder textual content inTextField
. - 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:
- Person inputs 2
- The
TextField
adjustments from0.00
to0.02
- Person inputs 9, the
TextField
then reads0.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!