I need to support pasting of images into a UITextView
. With an image copied to the clipboard, the "Paste
" option doesn't seem to pop up. It does when there's text on the clipboard.
This is how to override the paste
option in a custom UITextView
. But I need help on how to get the option to show up to begin 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 a few related questions, but they weren't helpful for an inexperienced developer like myself: How to get UIMenuController work for a custom view?, How to paste image from pasteboard on UITextView?
I answered my own question. All you have to do is have the UITextView say "I can receive 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];
}
You're welcome.