How to copy text to the clipboard in SwiftUI

In this tutorial, we will see the process of how to copy text to the clipboard in SwiftUI.

SwiftUI doesn’t provide direct clipboard manipulation. So, we can utilize the UIPasteboard class from UIKit to copy text to the clipboard in SwiftUI.

Now, follow the steps below to achieve this task.

Create a SwiftUI view with text

First of all, I will create a SwiftUI view that will contain the text that we want to copy to the clipboard.

Example

import SwiftUI

struct ContentView: View {
    let textToCopy = "Welcome to CodeSpeedy"

    var body: some View {
        Text(textToCopy)
            .padding()
    }
}

In the above code, I have created a Text view with the content set to the value of textToCopyvariable.

This will display the text that I have stored in the textToCopy variable.

Output:

Create a SwiftUI view with text

Create a button to copy text

Here, I will create a button to perform the copy operation. This button will trigger an action, which will call a function responsible for copying the text to the clipboard.

Example

Button(action: {
    // Action to copy text goes here
}) {
    Text("Copy to Clipboard")
}

In the above code, I have created a button using the Button view.

When the user taps the button, the code whatever we put within the button action closure will execute.

Output:

How to copy text to the clipboard in SwiftUI

Implement the copy function

This function will handle the actual copying of the text to the clipboard using the UIPasteboard class from UIKit.

Example

func copyToClipboard(text: String) {
    // Set the provided text to the general pasteboard to copy it to the clipboard
    UIPasteboard.general.string = text
}

In the above program, I have declared a function called copyToClipboard  with a parameter text of type String. This means we need to provide a string value as input when we call the function.

Then, the line UIPasteboard.general.string = text will set the provided text to the general pasteboard to copy the text to the clipboard.

When we call copyToClipboard with a specific string, it will copy that string to the clipboard and we can paste this copied string into other applications or contexts as per our requirement.

Now have a look at the complete code below.

import SwiftUI

struct ContentView: View {
    // Define the text that we want to copy to clipboard
    let textToCopy = "Welcome to CodeSpeedy"

    var body: some View {
        VStack {
            // Display the text that we want to copy
            Text(textToCopy)
                .padding()

            // Button to trigger the copy actio
            Button(action: {
                // Call the copyToClipboard function and pass textToCopy as an argument to copy the text to the clipboard.
                self.copyToClipboard(text: self.textToCopy)
            }) {
                Text("Copy to Clipboard")
            }
        }
    }

    // Function to copy text to the clipboard
    func copyToClipboard(text: String) {
        UIPasteboard.general.string = text
    }
}

The above code will display a simple text with a button, when we tap the button it will copy the specified text and we can paste the text into other applications.

Output:

How to copy text to the clipboard in SwiftUI

Leave a Reply

Your email address will not be published. Required fields are marked *