Updated for Xcode 14.2
SwiftUI’s TextField
supports placeholder text just like UITextField
did – gray text that is shown in the text field when it’s empty, either giving users a prompt (“Enter your password”) or showing some example data.
To set your placeholder text, pass it in as part of the initializer for your text field, like this:
struct ContentView: View {
@State private var emailAddress = ""
var body: some View {
TextField("johnnyappleseed@apple.com", text: $emailAddress)
.textFieldStyle(.roundedBorder)
.padding()
}
}
Download this as an Xcode project
Important: If you’re using Xcode 12 you need to use RoundedBorderTextFieldStyle()
rather than .roundedBorder
.
That will show “johnnyappleseed@apple.com” in the text field while it’s empty, but as soon as the user types something in there it disappears.
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.