微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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];
}

别客气.

原文地址:https://www.jb51.cc/iOS/328583.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐