Contents

AttributedTextFormatting.Transferable

A transferable representation of an attributed string interpreted in a SwiftUI environment.

Declaration

struct Transferable

Overview

Use this type e.g. with drag and drop APIs or to create a fileExporter(isPresented:item:contentTypes:defaultFilename:onCompletion:onCancellation:).

struct RichTextEditorView: View {
    @State private var text: AttributedString = ""
    @Environment(\.self) private var environment
    @State var fileExporterIsPresented: Bool = false

    var body: some View {
        TextEditor(text: $text)
            .toolbar {
                Button("Save") {
                    fileExporterIsPresented = true
                }
            }
            .fileExporter(
                isPresented: $fileExporterIsPresented,
                item: AttributedTextFormatting.Transferable(text: text, in: environment)
            ) { result in
                handleResult(result)
            }
            .dropDestination(
                for: AttributedTextFormatting.Transferable.self
            ) { transferables, _ in
                text.replaceSelection(
                    &selection,
                    with: transferables.map {
                        AttributedString(transferable: $0, in: environment)
                    }.joined(separator: AttributedString("\n")))
                return true
            }
    }
}

To extract text the text after importing, use attributed string’s Foundation/AttributedString/init(transferable:in:).

Supported content types include:

Topics

Initializers