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

我需要支持将图像粘贴到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];
}

别客气.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...