Updated for Xcode 12.0
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 uses a prompt (“Enter your password”) or showing some example data.
To set a placeholder, 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(RoundedBorderTextFieldStyle())
.padding()
}
}
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 Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.