ios – 如何启用UITextView以接收粘贴的图像

前端之家收集整理的这篇文章主要介绍了ios – 如何启用UITextView以接收粘贴的图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要支持将图像粘贴到UITextView中.将图像复制到剪贴板后,似乎不会弹出“粘贴”选项.当剪贴板上有文本时它会这样做.

这是如何在自定义UITextView中覆盖粘贴选项.但是我需要帮助才能让选项显示出来……

@H_403_4@// 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]; } }

我遇到了一些相关问题,但对于像我这样缺乏经验的开发人员来说,它们没有用处:
@L_403_0@,How to paste image from pasteboard on UITextView?

解决方法

我回答了自己的问题.你所要做的就是通过覆盖这个UITextView方法让UITextView说“我可以接收粘贴的图像”: @H_403_4@- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image) return YES; else return [super canPerformAction:action withSender:sender]; }

别客气.

猜你在找的iOS相关文章