How does one copy the contents of a Text field to the iOS clipboard?
I have the following code & want to replace the "print(..)" statement with a statement that copies the content of the text field to the iOS clipboard.
Text(self.BLEinfo.sendRcvLog)
.onTapGesture(count: 2) {
print("Copy text field content to ClipBoard Here..")
}
Can't seem to find any SwiftUI examples how to do this.
Thanks!
Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.
Use the following
import MobileCoreServices // << for UTI types
// ... other code
Text(self.BLEinfo.sendRcvLog)
.onTapGesture(count: 2) {
UIPasteboard.general.setValue(self.BLEinfo.sendRcvLog,
forPasteboardType: kUTTypePlainText as String)
}
Text(self.BLEinfo.sendRcvLog)
.onTapGesture(count: 2) {
UIPasteboard.general.string = self.BLEinfo.sendRcvLog
}
or really fancy:
Text(self.BLEinfo.sendRcvLog)
.contextMenu {
Button(action: {
UIPasteboard.general.string = self.BLEinfo.sendRcvLog
}) {
Text("Copy to clipboard")
Image(systemName: "doc.on.doc")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With