<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TextField",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI9TextFieldV"
  },
  "title" : "TextField"
}
-->

# TextField

A control that displays an editable text interface.

```
nonisolated struct TextField<Label> where Label : View
```

## Overview

You create a text field with a label and a binding to a value. If the
value is a string, the text field updates this value continuously as the
user types or otherwise edits the text in the field. For non-string types,
it updates the value when the user commits their edits, such as by pressing
the Return key.

The following example shows a text field to accept a username, and a
[`Text`](/documentation/SwiftUI/Text) view below it that shadows the continuously updated value
of `username`. The [`Text`](/documentation/SwiftUI/Text) view changes color as the user begins and ends
editing. When the user submits their completed entry to the text field,
the [`onSubmit(of:_:)`](/documentation/SwiftUI/View/onSubmit(of:_:)) modifier calls an internal `validate(name:)`
method.

```
@State private var username: String = ""
@FocusState private var emailFieldIsFocused: Bool = false

var body: some View {
    TextField(
        "User name (email address)",
        text: $username
    )
    .focused($emailFieldIsFocused)
    .onSubmit {
        validate(name: username)
    }
    .textInputAutocapitalization(.never)
    .disableAutocorrection(true)
    .border(.secondary)

    Text(username)
        .foregroundColor(emailFieldIsFocused ? .red : .blue)
}
```

![A text field showing the typed email mruiz2@icloud.com, with a text](images/com.apple.SwiftUI/SwiftUI-TextField-echoText@2x.png)

The bound value doesn’t have to be a string. By using a
<doc://com.apple.documentation/documentation/Foundation/FormatStyle>,
you can bind the text field to a nonstring type, using the format style
to convert the typed text into an instance of the bound type. The following
example uses a
<doc://com.apple.documentation/documentation/Foundation/PersonNameComponents/FormatStyle>
to convert the name typed in the text field to a
<doc://com.apple.documentation/documentation/Foundation/PersonNameComponents>
instance. A [`Text`](/documentation/SwiftUI/Text) view below the text field shows the debug description
string of this instance.

```
@State private var nameComponents = PersonNameComponents()

var body: some View {
    TextField(
        "Proper name",
        value: $nameComponents,
        format: .name(style: .medium)
    )
    .onSubmit {
        validate(components: nameComponents)
    }
    .disableAutocorrection(true)
    .border(.secondary)
    Text(nameComponents.debugDescription)
}
```

![A text field showing the typed name Maria Ruiz, with a text view below](images/com.apple.SwiftUI/SwiftUI-TextField-nameComponents@2x.png)

### Text field prompts

You can set an explicit prompt on the text field to guide users on what
text they should provide. Each text field style determines where and
when the text field uses a prompt and label. For example, a form on macOS
always places the label at the leading edge of the field and
uses a prompt, when available, as placeholder text within the field itself.
In the same context on iOS, the text field uses either the prompt or label
as placeholder text, depending on whether the initializer provided a prompt.

The following example shows a [`Form`](/documentation/SwiftUI/Form) with two text fields, each of which
provides a prompt to indicate that the field is required, and a content builder
to provide a label:

```
Form {
    TextField(text: $username, prompt: Text("Required")) {
        Text("Username")
    }
    SecureField(text: $password, prompt: Text("Required")) {
        Text("Password")
    }
}
```

![A macOS form, showing two text fields, arranged vertically, with labels to](images/com.apple.SwiftUI/TextField-prompt-1@2x.png)

![An iOS form, showing two text fields, arranged vertically, with prompt](images/com.apple.SwiftUI/TextField-prompt-2@2x.png)

### Styling text fields

SwiftUI provides a default text field style that reflects an appearance and
behavior appropriate to the platform. The default style also takes the
current context into consideration, like whether the text field is in a
container that presents text fields with a special style. Beyond this, you
can customize the appearance and interaction of text fields using the
[`textFieldStyle(_:)`](/documentation/SwiftUI/View/textFieldStyle(_:)) modifier, passing in an instance of
[`TextFieldStyle`](/documentation/SwiftUI/TextFieldStyle). The following example applies the
[`roundedBorder`](/documentation/SwiftUI/TextFieldStyle/roundedBorder) style to both text fields within a [`VStack`](/documentation/SwiftUI/VStack).

```
@State private var givenName: String = ""
@State private var familyName: String = ""

var body: some View {
    VStack {
        TextField(
            "Given Name",
            text: $givenName
        )
        .disableAutocorrection(true)
        TextField(
            "Family Name",
            text: $familyName
        )
        .disableAutocorrection(true)
    }
    .textFieldStyle(.roundedBorder)
}
```

![Two vertically-stacked text fields, with the prompt text Given Name and](images/com.apple.SwiftUI/SwiftUI-TextField-roundedBorderStyle@2x.png)

## Topics

### Creating a text field with a string

[`init(_:text:)`](/documentation/SwiftUI/TextField/init(_:text:))

Creates a text field with a text label generated from a localized title
string.

[`init(_:text:prompt:)`](/documentation/SwiftUI/TextField/init(_:text:prompt:))

Creates a text field with a text label generated from a localized title
string resource.

[`init(text:prompt:label:)`](/documentation/SwiftUI/TextField/init(text:prompt:label:))

Creates a text field with a prompt generated from a `Text`.

### Creating a scrollable text field

[`init(_:text:axis:)`](/documentation/SwiftUI/TextField/init(_:text:axis:))

Creates a text field with a preferred axis and a text label generated
from a localized title string resource.

[`init(_:text:prompt:axis:)`](/documentation/SwiftUI/TextField/init(_:text:prompt:axis:))

Creates a text field with a preferred axis and a text label generated
from a localized title string resource.

[`init(text:prompt:axis:label:)`](/documentation/SwiftUI/TextField/init(text:prompt:axis:label:))

Creates a text field with a preferred axis and a prompt generated from
a `Text`.

### Creating a text field with a value

Use these initializers to create a text field that binds to a value of an arbitrary type.

[`init(_:value:format:prompt:)`](/documentation/SwiftUI/TextField/init(_:value:format:prompt:))

Creates a text field that applies a format style to a bound
value, with a label generated from a localized title string resource.

[`init(value:format:prompt:label:)`](/documentation/SwiftUI/TextField/init(value:format:prompt:label:))

Creates a text field that applies a format style to a bound
value, with a label generated from a content builder.

[`init(_:value:formatter:)`](/documentation/SwiftUI/TextField/init(_:value:formatter:))

Create an instance which binds over an arbitrary type, `V`.

[`init(_:value:formatter:prompt:)`](/documentation/SwiftUI/TextField/init(_:value:formatter:prompt:))

Creates a text field that applies a formatter to a bound
value, with a label generated from a localized title string resource.

[`init(value:formatter:prompt:label:)`](/documentation/SwiftUI/TextField/init(value:formatter:prompt:label:))

Creates a text field that applies a formatter to a bound optional
value, with a label generated from a content builder.

### Deprecated initializers

[Deprecated initializers](/documentation/SwiftUI/TextField-Deprecated)

Review deprecated text field initializers.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)