Skip to main content

How to make TextField uppercase or lowercase using textCase()

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to make TextField uppercase or lowercase using textCase() 관련

SwiftUI by Example

Back to Home

How to make TextField uppercase or lowercase using textCase() | SwiftUI by Example

How to make TextField uppercase or lowercase using textCase()

Updated for Xcode 15

SwiftUI's TextField view normally lets users write their text in whatever case they want, but if you want to control that you can force either uppercase or lowercase text using the textCase() modifier.

For example, this asks users to enter their name and uppercases every letter:

struct ContentView: View {
    @State private var name = "Paul"

    var body: some View {
        TextField("Shout your name at me", text: $name)
            .textFieldStyle(.roundedBorder)
            .textCase(.uppercase)
            .padding(.horizontal)
    }
}

Download this as an Xcode projectopen in new window

A text box containing the word “PAUL”
A text box containing the word “PAUL”

Important

If you're using Xcode 12 you need to use RoundedBorderTextFieldStyle() rather than .roundedBorder.

Similar solutions…
How to make a TextField expand vertically as the user types | SwiftUI by Example

How to make a TextField expand vertically as the user types
How to make a TextField or TextEditor have default focus | SwiftUI by Example

How to make a TextField or TextEditor have default focus
How to dismiss the keyboard for a TextField | SwiftUI by Example

How to dismiss the keyboard for a TextField
How to add a TextField to an alert | SwiftUI by Example

How to add a TextField to an alert
How to format a TextField for numbers | SwiftUI by Example

How to format a TextField for numbers

이찬희 (MarkiiimarK)
Never Stop Learning.