TechnologiesSwiftUI

TextInputDictationBehavior struct

iOSmacOStvOSwatchOSvisionOS✓ renders

How it works

TextInputDictationBehavior describes how a text-entry view responds to dictation, letting you decide whether the dictation affordance (the microphone) is offered and when it activates. You apply it through the textInputDictationBehavior(_:) modifier on a text input such as TextField or TextEditor, rather than constructing the value directly. Reach for it when the default behavior isn't what you want, for example to favor keyboard entry in a field where speech-to-text would be noisy or imprecise, or simply to be explicit about supporting dictation. The behavior is inherited down the view hierarchy, so setting it on a container configures every text field it contains.

  1. Apply it with textInputDictationBehavior(_:)

    The behavior reaches a view through the textInputDictationBehavior(_:) modifier, which takes a TextInputDictationBehavior value and configures the dictation affordance for that text input. In the example the modifier is attached to the TextField bound to $note, declaring how that field treats dictation.

  2. Choose .automatic for the system default

    The .automatic value tells SwiftUI to use the platform's standard dictation behavior for the context, which on supported devices surfaces the microphone control so the user can speak their input. The example passes .automatic, opting the note field into the conventional dictation experience without overriding it.

  3. Suppress dictation where it doesn't fit

    Some inputs are better served by the keyboard alone, such as fields expecting codes, precise identifiers, or terse tokens. TextInputDictationBehavior provides a disabling option for exactly these cases, letting you turn the microphone off on a specific field while leaving the rest of your text inputs untouched.

  4. Scope the behavior by where you place the modifier

    Because the value propagates through the environment, attaching textInputDictationBehavior(_:) to a single TextField configures only that field, while attaching it higher up applies it to every text input beneath it. Here the modifier sits directly on the TextField inside the VStack, so the setting is scoped to just the note entry.

Try it — Change .textInputDictationBehavior(.automatic) to disable dictation on the field and observe the microphone control disappear from the TextField bound to $note.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

TextInputDictationBehavior.swift
struct TextInputDictationBehaviorDemo: View {
    @State private var note = ""
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Quick Note")
                .font(.headline)
            TextField("Tap mic to dictate", text: $note)
                .textFieldStyle(.roundedBorder)
                .textInputDictationBehavior(.automatic)
        }
        .padding()
    }
}
Live preview
Quick Note Tap mic to dictate
Quick Note Tap mic to dictate
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →