How to use AttributedString with TextEditor in SwiftUI


Greetings, traveler!

Since the introduction of multiline TextField in iOS 16, the TextEditor component in SwiftUI has often been overshadowed by other text input solutions. But with the release of iOS 26, Apple introduced support for AttributedString as a binding for TextEditor, along with a new formatting menu for selected text.

The integration of AttributedString allows developers to bind richly formatted text directly to a TextEditor. This enables dynamic updates to text attributes, such as font, color, or style, while maintaining compatibility with SwiftUI’s reactive architecture. Additionally, when users select text within a TextEditor, a contextual formatting menu now appears, offering options to adjust string attributes directly. This menu provides a native interface for users to modify text properties without requiring custom implementations.

Below is an example of how to implement a TextEditor with an AttributedString binding in SwiftUI:

import SwiftUI

struct ContentView: View {
    @State private var attributedString: AttributedString = .init()
    
    var body: some View {
        TextEditor(text: $attributedString)
            .padding()
    }
}

This code demonstrates a basic setup where an AttributedString is bound to a TextEditor, allowing the component to handle formatted text input.

Connclusion

These updates make TextEditor a more viable option for applications requiring rich text input, aligning it more closely with modern text editing expectations. Developers can now leverage these features to build interfaces that support complex text formatting without relying on third-party libraries or custom solutions.