init(supportedContentTypes : [UTType], payloadAction : ([NSItemProvider ]) -> Void)
init<Payload>(supportedContentTypes : [UTType], validator: ([NSItemProvider ]) -> Payload?, payloadAction : (Payload) -> Void)
Availability 有効性
Technology
struct PasteButton
Use a Paste
when you want to provide a button for pasting data from the system pasteboard into your app. The system provides a button appearance and label appropriate to the current environment; your app can’t change this visual presentation.
You use an array of UTType
instances to declare the UTIs of data types your app can handle. When the user clicks the button, your app receives the pasteboard data. You can also provide a validator closure to enable or disable the button based on the contents of the pasteboard.
In the following example, a Paste
declares that it accepts only plaintext, using the UTF-8 text encoding. When the user clicks the button, the sample’s closure attempts to retrieve plaintext from the provided array of NSItem
instances. If the closure successfully loads the plaintext data, it creates a string from the data and sets it as the value of pasted
, which updates an adjoining Text
view.
private var pastedText: String = ""
private let plainTextUTIIdentifier = UTType.utf8PlainText.identifier
var body: some View {
HStack {
PasteButton(
supportedContentTypes: [.utf8PlainText]
) { itemProviders in
itemProviders.first(where: {
$0.hasItemConformingToTypeIdentifier(
plainTextUTIIdentifier)})?
.loadDataRepresentation(forTypeIdentifier:
plainTextUTIIdentifier) { data, _ in
if let data = data,
let string = String(data: data, encoding: .utf8) {
pastedText = string
}
}
}
Divider()
Text(pastedText)
Spacer()
}
}
Keep in mind that Paste
doesn’t automatically validate and invalidate based on changes to the pasteboard. So instead of having a Paste
as a permanent part of your UI, you should use it in transient contexts like Context
.
init(supportedContentTypes : [UTType], payloadAction : ([NSItemProvider ]) -> Void)
init<Payload>(supportedContentTypes : [UTType], validator: ([NSItemProvider ]) -> Payload?, payloadAction : (Payload) -> Void)
var body: some View
typealias Body
init(supportedTypes : [String], payloadAction : ([NSItemProvider ]) -> Void)
init<Payload>(supportedTypes : [String], validator: ([NSItemProvider ]) -> Payload?, payloadAction : (Payload) -> Void)
struct Button
struct EditButton
struct Link
struct Menu