我需要支持将图像粘贴到UITextView中.将图像复制到剪贴板后,似乎不会弹出“粘贴”选项.当剪贴板上有文本时它会这样做.
这是如何在自定义UITextView中覆盖粘贴选项.但是我需要帮助才能让选项显示出来……
// 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]; } }
我遇到了一些相关问题,但对于像我这样缺乏经验的开发人员来说,它们没有用处:
How to get UIMenuController work for a custom view?,How to paste image from pasteboard on UITextView?
解决方法
我回答了自己的问题.你所要做的就是通过覆盖这个UITextView方法让UITextView说“我可以接收粘贴的图像”:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image) return YES; else return [super canPerformAction:action withSender:sender]; }
别客气.