Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Total Context
I start with the under View, which provides me the consequence picture of “Complete 0.00” that follows.
My downside: 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:
0
within the 2nd decimal place of the placeholder textual content in TextField
.Instance: To illustrate the consumer’s buy worth was 29.50:
TextField
modifications from 0.00
to 0.02
TextField
then reads 0.29
This continues till the consumer finishes inputing 29.50.
TLDR I need the enter to run proper to left, conserving the decimal within the applicable place.
import SwiftUI
struct ContentView: View {
@State non-public var whole: String = "0.00"
var physique: some View {
NavigationView {
Type {
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 discovered .onEditingChanged
is just not a property of TextField
so I wanted to strive one other strategy….
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 consequence = formatter.string(from: NSNumber(worth: Double(worth) ?? 0)) {
self.whole = consequence
}
}
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 consequence = formatter.string(from: NSNumber(worth: Double(self.whole) ?? 0)) {
self.whole = consequence
}
}
I’m at a loss as easy methods to obtain my aim. Thanks for the assistance prematurely!