UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Unable to force a textfield to be lower case

Forums > SwiftUI

I have the following textfield but when I enter the token the first character is always uppercase even though i've specified the lowercase modifier. Any ideas?

    @State private var token: String = "xyz"
    ...
    ...

                 Section {
                        TextField("Token", text: $token)
                            .textCase(.lowercase)
                            .foregroundColor(.black)
                            .font(.headline)
                            .textFieldStyle(OvalTextFieldStyle())
                    } header: {
                        Text("Your Personal Token")
                            .font(.headline)
                    }

1      

I think the solution may more be related to the keyboard modifiers. The shift key is ON by default. I want it to be OFF but I can't see how thats done.

1      

Autocapitalization is usually due to a device setting. I would check the settings for your simulator.

You can also use the autocapitalization(_:) modifier. (That modifier was deprecated in favor of textInputAutocapitalization(_:) with iOS 15).

1      

Thank you. SOLVED:

                            .textInputAutocapitalization(.never)

1      

@roosterboy is correct and while this would take away the first cap letter in the text field do not guarantee that the user do not use the shift button and you would end up with caps in the token

It might good idea to ensure that the string is in the format that you want.

struct ContentView: View {
    @State private var token: String = ""

    var lowercasedToken: String {
        token.lowercased()
    }

    var uppercasedToken: String {
        token.uppercased()
    }

    var capitalizedToken: String {
        token.capitalized
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 30) {

            TextField("Token", text: $token)
                .textFieldStyle(.roundedBorder)
                .textInputAutocapitalization(.never)

            Text("Result lowercase: \(lowercasedToken)")

            Text("Result uppercase: \(uppercasedToken)")

            Text("Result capitalized: \(capitalizedToken)")

        }
        .padding()
    }
}

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.