TextField
Published Jan 20, 2023
Contribute to Docs
TextField
is a view that allows the user to fill in the field with data, such as contact information or memos.
Syntax
var body: some View {
TextField("Text field description", text: bindingParameter)
}
- The
"Text field description"
string refers to the text that will be displayed with a lighter color inside the field. text:
is passed abindingParameter
so that Swift will always know if and when this parameter changes.
Example
In the example below, a VStack
holds a series of TextField
s and one SecureField
, which represents a sign-up page:
@State private var name = ""@State private var lastName = ""@State private var username = ""@State private var email = ""@State private var password = ""var body: some View {VStack {TextField("Name", text: $name)TextField("Last Name", text: $lastName).keyboardType(.default)TextField("Username", text: $username).keyboardType(.asciiCapable)TextField("Email", text: $email).keyboardType(.emailAddress)SecureField("Password", text: $password)}.padding()}
- The declared variables are marked with the
private
keyword which only allows them to be accessed within the module that they are created in. TextField
not only has different keyboard types but also keyboard styles. This includes different keyboards per style or type (e.g., email format, number pad, or ASCII). If no type or style is specified, Swift will apply the.default
modifier which can differ depending on the operating system and device’s default style settings.SecureField
is a text field view that instantly hides characters and is commonly used for password input.
This will display an empty text field:
This will display a filled text field:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.