I have two questions:
I'm using a decimalpad keyboard and when I use the comma the app doesn't recognise it
How do I dismiss the keyboard?
(Can you please write the solution if you know it, thank you for your time)
Code:
Code Block import SwiftUI struct BMIView: View { @State private var height = "" @State private var weight = "" @Environment(\.presentationMode) var presentationMode var inputAfterConvertions: Float { let hh = Float(height) ?? 0 let ww = Float(weight) ?? 0 var ris: Float = 0 if hh > 0 && ww > 0{ ris = (ww / (hh * hh)) * 1000 return ris } return 0 } var health: String{ if inputAfterConvertions < 18.49 && inputAfterConvertions > 0 { return "" } else if inputAfterConvertions > 18.5 && inputAfterConvertions < 24.99{ return "" } else if inputAfterConvertions > 25{ return "" } return "" } @State var isWriting: Bool = false var body: some View { NavigationView{ Form{ Section(header: Text("Enter your height in cm")){ TextField("Input",text: $height) .keyboardType(.decimalPad) } Section(header: Text("Enter your Weight in kg")){ TextField("Input",text: $weight) .keyboardType(.decimalPad) } Section(header: Text("Check result")){ Text("\(inputAfterConvertions, specifier: "%.2f")") } } .navigationBarTitle("BMI") .navigationBarItems(trailing: Button(action: { presentationMode.wrappedValue.dismiss() }) { Image(systemName: "xmark").font(.title).foregroundColor(.blue) } ) } } }
Thank you very much for your time
I'm using a decimalpad keyboard and when I use the comma the app doesn't recognise it
Don't you think it is better for users that they get localized, familiar keypad shown?I want that in every country the decimals are written with the comma ( , )
Unless you are creating sort of a programming tool, decimal separator should be localized, I think.
You need to do 3 things consistent, when you work with numeric values in UI.
Show properly localized keypad (iOS will manage it, and I do not know how to override the behavior of iOS)
Read the numeric values using NumberFormatter properly localized
Show the numeric values using NumberFormatter properly localized
Code Block struct BMIView: View { let numberFormatter: NumberFormatter = { let nf = NumberFormatter() nf.locale = Locale.current //Set up the NumberFormatter as you like... nf.numberStyle = .decimal nf.maximumFractionDigits = 1 return nf }() @State private var height = "" @State private var weight = "" @Environment(\.presentationMode) var presentationMode var inputAfterConvertions: Float { //Use NumberFormatter to read numeric values let hh = numberFormatter.number(from: height)?.floatValue ?? 0 //<- let ww = numberFormatter.number(from: weight)?.floatValue ?? 0 //<- var ris: Float = 0 if hh > 0 && ww > 0{ ris = (ww / (hh * hh)) * 1000 return ris } return 0 } //... var body: some View { NavigationView{ Form{ Section(header: Text("Enter your height in cm")){ TextField("Input",text: $height) .keyboardType(.decimalPad) } Section(header: Text("Enter your Weight in kg")){ TextField("Input",text: $weight) .keyboardType(.decimalPad) } Section(header: Text("Check result")){ //Use NumberFormatter to show numeric values Text("\(inputAfterConvertions as NSNumber, formatter: numberFormatter)") //<- } } //... }
Seems you are trying something called DecimalTextField (regarding issue #2), but you should better always have in mind localization issues unless you write U.S. only apps.
Sorry, but I do not know the right solution in pure SwiftUI. Maybe something like UIViewRepresentable (as found in DecimalTextField) would be needed.How do I dismiss the keyboard?
One more.
If you already have a similar thread and it is unsolved, you should better consider continuing on the old thread. It is not good for the forums to have many threads unsolved.