A transferable representation of an attributed string interpreted in a
SwiftUI environment.
Use this type e.g. with drag and drop APIs or to create a
``View/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:
- <doc://com.apple.documentation/documentation/uniformtypeidentifiers/uttype-swift.struct/rtfd>
- <doc://com.apple.documentation/documentation/uniformtypeidentifiers/uttype-swift.struct/rtf>
init(text: AttributedString, in environment: EnvironmentValues)
Create a transferable representation of an attributed string
as interpreted in a SwiftUI environment.
When exporting the `text` into different data formats,
the transfer representation may use the given `environment` to
resolve semantic attribute values, such as certain colors or fonts
to concrete values. This means that depending on the representation
used during transfer, some semantic information may be lost in that
step.
- Note: The transferable representation applies the
``AttributedTextFormattingDefinition`` in the `environment` before
exporting the content.
- SeeAlso: ``View/attributedTextFormattingDefinition(_:)-uc57``,
``AttributedTextValueConstraint/constrain(_:)-6cp64``
Skipped declarations
1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.