<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TextField/init(text:selection:prompt:axis:label:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI9TextFieldV4text9selection6prompt4axis5labelACyxGAA7BindingVySSG_AKyAA0C9SelectionVSgGAA0C0VSgAA4AxisOSgxyXEtcfc"
  },
  "title" : "init(text:selection:prompt:axis:label:)"
}
-->

# init(text:selection:prompt:axis:label:)

Creates a text field with a binding to the current selection and a prompt generated from a `Text`.

```
nonisolated init(text: Binding<String>, selection: Binding<TextSelection?>, prompt: Text? = nil, axis: Axis? = nil, @ContentBuilder label: () -> Label)
```

## Parameters

`text`

The text to display and edit.

`selection`

A [`Binding`](/documentation/SwiftUI/Binding) to the variable containing the selection.

`prompt`

A `Text` representing the prompt of the text field
which provides users with guidance on what to type into the text
field. Defaults to `nil`.

`axis`

The axis in which to scroll text when it doesn’t fit
in the available space. Defaults to `nil`.

`label`

A view that describes the purpose of the text field.

## Discussion

The following example shows a text field with a binding to the current selection:

```
@State private var message: String = ""
@State private var selection: TextSelection? = nil

var body: some View {
    TextField(text: $message, selection: $selection) {
        Text("Message")
    }
}
```

Use the [`onSubmit(of:_:)`](/documentation/SwiftUI/View/onSubmit(of:_:)) modifier to invoke an action
whenever the user submits this text field.

---

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)