6

I have the following code on a keyboard extensión

let pasteboard = UIPasteboard.generalPasteboard()
var image = UIImage(named: "myimage");
pasteboard.image = image;

This doesn't work on a UITextView I have on my container application, paste context menu never shows up. It works on other applications like "messages" but not on mine.

My code works if I try to paste text instead of an image using string property so I'm quite near.

I could need to set up my text view different but I don't know how. I've changed "Text" from "Plain" to "Attributed" but still not working.

vrwim
  • 13,020
  • 13
  • 63
  • 118
StackOverflower
  • 5,463
  • 13
  • 58
  • 89

3 Answers3

9

It doesn't work if implemented in UITextView subclass, but I tried it in the UIViewController containing the textView and it worked:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(paste:)) {
        return [UIPasteboard generalPasteboard].string != nil || [UIPasteboard generalPasteboard].image != nil;
        //if you want to do this for specific textView add && [yourTextView isFirstResponder] to if statement
    }

    return [super canPerformAction:action withSender:sender];

}

-(void)paste:(id)sender {
    //do your action here
}
Frane Poljak
  • 2,315
  • 23
  • 25
8

UITextView only supports pasting text out of the box. You can subclass it and add support for pasting images, which can be implemented using attributed string text attachments.

NSHipster's writeup on UIMenuController and this Stack Overflow question explain the paste logic.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks for your answer. Then any of the text controls where the image paste works are custom subclasses and not built in controls? – StackOverflower Mar 20 '15 at 20:44
  • It's correct that none of them are built-in controls. They might be something besides custom subclasses, but a subclass is how I'd implement it in your case. – Aaron Brager Mar 20 '15 at 20:55
5

Create an NSTextAttachment from the image and an attributed string with the TextAttachment. Then set the attributedText property of the UITextView. Subclass UITextView and override the paste(_:) method:

override func paste(_ sender: Any?) {
    let textAttachment = NSTextAttachment()
    textAttachment.image = UIPasteboard.general.image
    attributedText = NSAttributedString(attachment: textAttachment)
}
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • mhhh, but how do I trigger this if "paste" context menu never shows up? – StackOverflower Mar 20 '15 at 20:43
  • @TimmyO'Tool edited my answer. You have to subclass UITextView and override the paste function. – Nikos M. Mar 20 '15 at 20:45
  • Thanks, I'll be checking about this. In any case I didn't want t paste in my application, just wanted to be sure that I implemented correctly the copy feature to make it work with third party applications. Now I know that the problems is that UITextView doesn't support this out of the box and not a bug on my code. – StackOverflower Mar 20 '15 at 20:50
  • 1
    I'm confused. What was the answer to @StackOverflower's question, "but how do I trigger this if 'paste' context menu never shows up?" – Matt Koala Dec 16 '15 at 04:47
  • I cannnot override paste for uitextview https://stackoverflow.com/questions/73739276/how-can-i-override-paste-so-i-can-paste-images-in-a-uitextview – Nat Serrano Sep 16 '22 at 03:11
  • I got an error that I cannot override : https://stackoverflow.com/questions/73739276/how-can-i-override-paste-so-i-can-paste-images-in-a-uitextview – Nat Serrano Sep 16 '22 at 03:14