4

I am using TextEditor with the String type right now but I want to use AttributedString because I want to find every character that starts with a # symbol and mark it a different color using range.

How can I use AttributedString with TextEditor?

struct PlaygroundView: View {
    @State private var text = AttributedString("")
    var body: some View {
        TextEditor(text: text)
    }
}

returns to me Cannot convert value of type 'AttributedString' to expected argument type 'Binding<String>'

5
  • SwiftUI's TextEditor doesn't support AttributedStrings Commented Nov 1, 2022 at 5:21
  • Is there a way I can work around that or is there another view that would offer me multiline text capability? Eventually I also want to have embedded images and other content types as well. Not sure if there's a generic view. Commented Nov 1, 2022 at 5:54
  • 2
    You'd need to use UITextView Commented Nov 1, 2022 at 5:58
  • You can check out my RichTextKit, which wraps UITextView and NSTextView in a SwiftUI RichTextEditor: github.com/danielsaidi/RichTextKit Commented Jul 2, 2024 at 5:35
  • iOS 26 added support. Commented Aug 7 at 3:16

2 Answers 2

9

As of now SwiftUI TextEditor doesn't support AttributedStrings, to work around this, you might consider using a UIViewRepresentable to wrap a UITextView

import SwiftUI

struct AttributedTextEditor: UIViewRepresentable {
    @Binding var text: AttributedString

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.delegate = context.coordinator
        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.attributedText = text
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, UITextViewDelegate {
        var parent: AttributedTextEditor

        init(_ parent: AttributedTextEditor) {
            self.parent = parent
        }

        func textViewDidChange(_ textView: UITextView) {
            parent.text = AttributedString(textView.attributedText)
        }
    }
}

and you can use it in your SwiftUI view like this:

AttributedTextEditor(text: $attributedText)
        .onChange(of: attributedText, perform: { value in
            detectMentions()
    })
Sign up to request clarification or add additional context in comments.

Comments

0

iOS/macOS 26+

The SwiftUI TextEditor now officially supports AttributedStrings using the new init(text:selection:) initializer.

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

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

1 Comment

Note: this is currently available in the Beta versions of the OS only, not the default release version that installs when you download Xcode. It will probably be available in MacOS Tahoe and iOS / iPad 26.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.