I have a text-view with some text and a copy button in that view,
When the user enters some text and presses the copy button, it needs to copy that text and paste that text wherever he wants.
I know there is a default copy/paste menu-controller in iOS, but I want to do this functionality in a button click. I think there is UIPasteboard
to do this functionality, but I don't know how to use it.
To paste the copied content onto the screen, perform a pinch-out or pinch-open gesture on the screen using three of your fingers (most preferably, thumb + index + middle fingers or index + middle + ring fingers).
To copy from a button click:
- (IBAction)copy { UIPasteboard *pb = [UIPasteboard generalPasteboard]; [pb setString:[textView text]]; }
To paste from a button click:
- (IBAction)paste { UIPasteboard *pb = [UIPasteboard generalPasteboard]; textView.text = [pb string]; }
This is the Swift version of the accepted answer.
Copy
UIPasteboard.general.string = myTextView.text
Paste
if let myString = UIPasteboard.general.string { myTextView.insertText(myString) }
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