How to enable UITextView to get pasted images

I need to support inserting images in a UITextView . If the image is copied to the clipboard, the " Paste " option does not appear. This happens when there is text on the clipboard.

Here's how to override the Paste parameter in a custom UITextView . But I need help on how to get an opportunity to seem to start with ...

 // This gets called when user presses menu "Paste" option - (void)paste:(id)sender{ UIImage *image = [UIPasteboard generalPasteboard].image; if (image) { NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = image; NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:textAttachment]; self.attributedText = imageString; } else { // Call the normal paste action [super paste:sender]; } } 

I came across several related questions, but they didn’t help for an inexperienced developer like me: How do I get UIMenuController to work for a custom view? How to insert an image from a cardboard table into a UITextView? p>

+6
source share
1 answer

I answered my question. All you have to do is get UITextView to say “I can get pasted images” by overriding this UITextView method:

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image) return YES; else return [super canPerformAction:action withSender:sender]; } 

Welcome.

+7
source

All Articles