HomeiOS DevelopmentSwiftUI TextField with quantity format ignores surroundings locale

SwiftUI TextField with quantity format ignores surroundings locale


I am dealing with the difficulty that SwiftUI’s TextField with a quantity format appears to disregard the locale set by the surroundings modifier.

struct MyView: View {

    @State
    var quantity: Decimal = 1.1

    var physique: some View {
        VStack {
            HStack(spacing: 20) {
                Textual content("Locale")
                    .body(width: 100)
                Textual content("Textual content")
                    .body(width: 100)
                Textual content("TextField")
                    .body(width: 100)
            }
            HStack(spacing: 20) {
                Textual content("de_DE")
                    .body(width: 100)
                Textual content(quantity, format: .quantity)
                    .body(width: 100)
                TextField("", worth: $quantity, format: .quantity)
                    .body(width: 100)
            }
            .surroundings(.locale, Locale(identifier: "de_DE"))
            HStack(spacing: 20) {
                Textual content("en_US")
                    .body(width: 100)
                Textual content(quantity, format: .quantity)
                    .body(width: 100)
                TextField("", worth: $quantity, format: .quantity)
                    .body(width: 100)
            }
            .surroundings(.locale, Locale(identifier: "en_US"))
        }
        .textFieldStyle(.roundedBorder)
    }
}

#Preview {
    MyView()
}

That is what the preview exhibits:

SwiftUI TextField with quantity format ignores surroundings locale

The primary row with german locale "de_DE" is right, as a result of in Germany they use comma as a separator, however the second row with english US locale it ought to present the dot within the textfield.
My pc’s locale area is Germany and I assume the TextField’s internals do not respect the surroundings locale and makes use of the system locale as an alternative.

So how do I make the TextField respect the surroundings’s locale? Is there a easy means with out doing all of the formatting your self?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments