TechnologiesSwiftUI

TextFormattingCommands struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A built-in set of commands for transforming the styles applied to selections

How it works

TextFormattingCommands is a built-in command group that contributes the standard text-styling actions — Bold, Italic, Underline — to your app's menu bar, typically under a Format menu. Rather than defining these familiar commands and their keyboard shortcuts by hand, you insert this one value into a scene's command set and SwiftUI supplies the menu items and their default behavior. Reach for it when your app edits styled text and you want the platform-conventional formatting menu without rebuilding it yourself.

  1. Add the group with the commands(content:) scene modifier

    TextFormattingCommands is a CommandsContent type, so you attach it to a scene — most often a WindowGroup — using the commands(content:) modifier. As the demo notes, you write .commands { TextFormattingCommands() } so the menu bar gains the formatting items when the app launches.

  2. Instantiate it with TextFormattingCommands()

    The group is created with its plain parameterless initializer, TextFormattingCommands(). There is nothing to configure: the value itself declares the full set of standard formatting commands, so a single instance is all you place inside the commands builder.

  3. Get Bold, Italic, and Underline for free

    Once inserted, the group populates the Format menu with the conventional styling actions and their system keyboard shortcuts. These map to the same styling a view expresses through modifiers like .bold(bold) and .italic(italic) on the Text("The quick brown fox") in the example, keeping menu commands and on-screen formatting consistent.

  4. Compose it alongside other command groups

    Because it conforms to Commands, TextFormattingCommands can sit beside other groups in the same builder, letting you assemble a full menu bar from standard and custom pieces. The Toggle("Bold", isOn: $bold) and Toggle("Italic", isOn: $italic) controls in the body show the equivalent in-window switches the menu items would drive.

Try it — Wrap the demo in a WindowGroup scene and attach .commands { TextFormattingCommands() }, then open the running app's menu bar to see the Bold, Italic, and Underline items appear under Format.

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.

TextFormattingCommands.swift
struct TextFormattingCommandsDemo: View {
    @State private var bold = false
    @State private var italic = false
    var body: some View {
        VStack(spacing: 16) {
            Text("Format Menu")
                .font(.headline)
            Text("The quick brown fox")
                .bold(bold)
                .italic(italic)
            Toggle("Bold", isOn: $bold)
            Toggle("Italic", isOn: $italic)
            Text("TextFormattingCommands adds Bold/Italic/Underline items to the Format menu via .commands { TextFormattingCommands() }")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Format Menu The quick brown fox Bold Italic TextFormattingCommands adds Bold/Italic/Underline items to the Format menu via .commands { TextFormattingCommands() }
Format Menu The quick brown fox Bold Italic TextFormattingCommands adds Bold/Italic/Underline items to the Format menu via .commands { TextFormattingCommands() }
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →