46

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
ICoder
  • 1,347
  • 2
  • 13
  • 23

5 Answers5

115

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];
}
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100
  • Only part of the text is selected in the text view. How can copy and paste only selected instead of copying whole thing. – Star May 25 '12 at 06:39
  • This will work if you are happy to always replace the existing contents of the textview. If you want to paste at the cursor position this is not sufficient. – Ben Packard Aug 30 '13 at 16:00
24

Swift

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)
}
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
7

For developers using MonoTouch, here are the two lines I used to complete the task in C#.

The answer iscavenger provided to this question served as the model for my answer (after I successfully implemented it in my project ;-)

UIPasteboard clipboard = UIPasteboard.General;
clipboard.String =  "string being added to clipboard";
benhorgen
  • 1,928
  • 1
  • 33
  • 38
6

Not sure why we can't simply use:

[theTextView paste:nil];

as per UIResponder docs

Ben Packard
  • 26,102
  • 25
  • 102
  • 183
4

I suspect you can relatively easily do what you want, starting with the [UIPasteboard dataForPasteboardType:] method.

There's Apple sample code you can look into at:

http://developer.apple.com/library/ios/#samplecode/CopyPasteTile/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009040

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Add in some code into your original question that shows how you are firing a button action and then taking the data from the pasteboard using the `[UIPasteboard dataForPasteboardType:]` method. If it still doesn't work for you, I can try to edit my answer to show you how it will work. – Michael Dautermann Nov 08 '11 at 05:06
  • i put this in my code but after taping the copy button the app crashes,-(IBAction)_clickbtncopynotes:(id)sender { [UIPasteboard dataForPasteboardType:textView.text]; } – ICoder Nov 08 '11 at 05:12
  • That's not the way to call the code (it's just a standard way to describe the likely method you would be using). You'd probably need to do something like `textView.text = [[UIPasteboard generalPasteboard] dataForPasteboardType: @"public.utf8-plain-text "]`. Please take a good look at the sample from Apple that I pointed you to and see how they retrieve data from the pasteboard. – Michael Dautermann Nov 08 '11 at 05:25
  • Please check my comment for previous answer and try to answer it – Star May 25 '12 at 06:40