<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "visionOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/TextEditor/init(text:selection:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI10TextEditorV4text9selectionAcA7BindingVy10Foundation16AttributedStringVG_AGyAA0iC9SelectionVGSgtcfc::OverloadGroup"
  },
  "title" : "init(text:selection:)"
}
-->

# init(text:selection:)

Creates a styled text editor.

```
nonisolated init(text: Binding<AttributedString>, selection: Binding<AttributedTextSelection>? = nil)
```

## Parameters

`text`

A [`Binding`](/documentation/SwiftUI/Binding) to the variable containing the
styled text to edit.

`selection`

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

## Discussion

Use a [`TextEditor`](/documentation/SwiftUI/TextEditor) instance to create a view in which users can enter
and edit long-form styled text.

In this example the text editor is setup to edit styled text:

```
struct StyledTextEditingView: View {
    @State private var text =
        AttributedString("This is some editable text...")

    var body: some View {
        TextEditor(text: $text)
    }
}
```

### Format text by combining attributes and view modifiers

If the AttributedString does not have a font and/or foreground color
specified for a given range of text, the rich text editor will use the
font and/or foreground color inherited from the environment for that
range of text, just like [`init(_:)`](/documentation/SwiftUI/Text/init(_:)-1a4oh). To
control what formatting options are available in the editor, use the
[`AttributedTextFormattingDefinition`](/documentation/SwiftUI/AttributedTextFormattingDefinition) protocol.

> Note: Other than ``doc://com.apple.SwiftUI/documentation/SwiftUI/Text``, this editor does not automatically
> translate UIKit or AppKit formatting attributes into SwiftUI attributes.
> For importing RTF documents, which encode formatting using UIKit and
> AppKit attributes, use ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextFormatting/Transferable``
> to obtain an attributed string compatible with this editor.

### Build custom controls using the selection binding

Use [`AttributedTextSelection`](/documentation/SwiftUI/AttributedTextSelection) for implementing custom controls, e.g.
for applying formatting such as boldness:

```
struct StyledTextEditingView: View {
    @State private var text: AttributedString = ""
    @State private var selection = AttributedTextSelection()

    @Environment(\.fontResolutionContext) private var fontResolutionContext

    var body: some View {
        TextEditor(text: $text, selection: $selection)
            .toolbar {
                // A toggle controlling whether the current selection in the
                // editor has bold font.
                Toggle(
                    "Toggle Boldness",
                    systemImage: "bold",
                    isOn: Binding(get: {
                        // Get the font for the current selection.
                        let font = selection.typingAttributes(in: text).font
                        // Resolve the font in the current environment.
                        let resolved = (font ?? .default).resolve(in: fontResolutionContext)
                        // Return whether the resolved font is bold.
                        return resolved.isBold
                    }, set: { isBold in
                        // Update each run in the current selection, including
                        // the typing attributes, to reflect the new `isBold`
                        // value.
                        text.transformAttributes(in: &selection) {
                            // Override the boldness of the font. If no font is
                            // present, use `Font.default` for the effective
                            // environment font as the basis.
                            $0.font = ($0.font ?? .default).bold(isBold)
                        }
                    })
                )
            }
    }
}
```

> Note: When binding the `selection`, always make sure it is updated
> after you mutate the `text`. Otherwise, the editor resets the selection
> to the end of the `text`. For more details, see
> ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextSelection/indices(in:)``.

> SeeAlso: ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextSelection``,
> ``View/attributedTextFormattingDefinition(_:)-uc57``,
> ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextFormattingDefinition``,
> ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextValueConstraint``, ``doc://com.apple.SwiftUI/documentation/SwiftUI/AttributedTextFormatting``

---

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)