init(text:selection:prompt:axis:label:)
Creates a text field with a binding to the current selection and a prompt generated from a Text.
Declaration
nonisolated init(text: Binding<String>, selection: Binding<TextSelection?>, prompt: Text? = nil, axis: Axis? = nil, @ViewBuilder label: () -> Label)Parameters
- text:
The text to display and edit.
- selection:
A Binding to the variable containing the selection.
- prompt:
A
Textrepresenting the prompt of the text field which provides users with guidance on what to type into the text field. Defaults tonil. - 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:_:) modifier to invoke an action whenever the user submits this text field.