PasteButton

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

ComponentiOS 16.0+macOS 10.15+PasteButton in SwiftUI

Declaration

React
import { PasteButton } from 'swiftui-react-native';
Swift
nonisolated struct PasteButton

Initializers

One SwiftUI initializer backs <PasteButton>.

React
<PasteButton
  payloadType: SwiftUnsupported<'T.Type'>
  onPaste: (arg0: any[]) => void
/>
Swift
nonisolated init<T>(
    payloadType: T.Type,
    onPaste: @escaping ([T]) -> Void
)
where T : Transferable
  • payloadType*SwiftUnsupported<'T.Type'>

    The type that you want to paste via the PasteButton.

  • onPaste*(arg0: any[]) => void

    The handler to call on trigger of the button with at least one item of the specified Transferable type from the pasteboard.

Also takes the common view props: modifiers, style and testID.

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 View.buttonBorderShape(_:), View.labelStyle(_:), and View.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 doc://com.apple.documentation/documentation/coretransferable/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()
    }
}

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

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Controls.