Updated for Xcode 14.0 beta 1
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 project
Important: If you’re using Xcode 12 you need to use RoundedBorderTextFieldStyle()
rather than .roundedBorder
.
SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.