Contents

PasteButton

A system button that reads items from the pasteboard and delivers it to a closure.

Declaration

@MainActor @preconcurrency struct PasteButton

Overview

Use a paste button when you want to provide a button for pasting items from the system pasteboard into your app. The system provides a button appearance and label appropriate to the current environment. However, you can use view modifiers like buttonBorderShape(_:), labelStyle(_:), and tint(_:) to customize the button in some contexts.

You declare what type of items your app will accept; use a type that conforms to the Transferable protocol. When the user taps or clicks the button, your closure receives the pasteboard items in the specified type.

In the following example, a paste button declares that it accepts a string. When the user taps or clicks the button, the sample’s closure receives an array of strings and sets the first as the value of pastedText, which updates a nearby Text view.

@State private var pastedText: String = ""

var body: some View {
    HStack {
        PasteButton(payloadType: String.self) { strings in
            pastedText = strings[0]
        }
        Divider()
        Text(pastedText)
        Spacer()
    }
}

[Image]

A paste button automatically validates and invalidates based on changes to the pasteboard on iOS, but not on macOS.

Topics

Creating a paste button

Deprecated initializers

See Also

Creating special-purpose buttons