How it works
TextInputFormattingControlPlacement identifies *where* SwiftUI presents the rich-text formatting controls — the bold, italic, list, and alignment affordances — that accompany an editable AttributedString. When a view edits formatted text, the system can offer these controls in more than one location, and this type names each of those locations so you can address them individually. Reach for it whenever you need to control the visibility of formatting affordances for a specific placement rather than for the text editor as a whole, passing it as the target of textInputFormattingControlVisibility(_:for:).
Edit formatted text that needs formatting controls
TextInputFormattingControlPlacement only becomes relevant once a view edits an AttributedString, since plain strings carry no formatting to toggle. Here a
TextEditoris bound to$text, whose@Statevalue is anAttributedString, which is what makes the rich-text formatting controls applicable to this editor.Name a placement with the static members
A value of TextInputFormattingControlPlacement designates one location where the controls can appear. You don't construct it directly; you select a placement through its provided members — in the example,
.inline, which refers to the formatting controls shown alongside the text being edited rather than in a separate bar.Target that placement with the
for:parameterThe placement value is consumed by the
textInputFormattingControlVisibility(_:for:)modifier, whosefor:argument is a TextInputFormattingControlPlacement. Writing.for: .inlinescopes the modifier so it governs only the inline controls, leaving any other placement to its default behavior.Pair it with a visibility to show or hide that location
TextInputFormattingControlPlacement says *which* controls; the accompanying
Visibilitysays *whether* they appear. Combined as.textInputFormattingControlVisibility(.visible, for: .inline), the modifier forces the inline formatting controls to be shown for thisTextEditor.
.visible to .hidden in .textInputFormattingControlVisibility(.visible, for: .inline) and watch the inline formatting controls disappear while the same .inline placement keeps the modifier scoped to exactly that location.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.
struct TextInputFormattingControlPlacementDemo: View {
@State private var text = AttributedString("Edit this rich text…")
var body: some View {
TextEditor(text: $text)
.frame(height: 160)
.border(.secondary)
.textInputFormattingControlVisibility(.visible, for: .inline)
.padding()
}
}