如何将菜单选项添加到NSTextAttachment弹出菜单是UITextView?

邓肯·格罗内瓦尔德(Duncan Groenewald)

我想在默认的图像附件菜单选项(复制图像,保存到相机胶卷)中添加另一个菜单选项。请注意,如果textView不在编辑模式下,则长按UITextView中嵌入的图像时会显示这些选项。

我尝试将自定义菜单添加到uimenucontroller并使用-(void)canPerformAction启用或禁用该选项,但是这似乎将菜单项添加到uitextView的编辑菜单中,并且对附件弹出菜单没有影响。

长按图像附件时,似乎永远不会调用-(void)canPerformAction。

邓肯·格罗内瓦尔德(Duncan Groenewald)

根据苹果公司的说法,没有公共API可以执行此操作,但是事实证明,将默认菜单替换为外观和行为相同的菜单是相对简单的。

在包含UITextView的viewController中,添加以下内容或类似内容并将其设置为textView的委托。

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange {

    // save in ivar so we can access once action sheet option is selected
    _attachment = textAttachment;

    [self attachmentActionSheet:(UITextView *)textView range:characterRange];

    return NO;
}
- (void)attachmentActionSheet:(UITextView *)textView range:(NSRange)range {

    // get the rect for the selected attachment (if its a big image with top not visible the action sheet
    // will be positioned above the top limit of the UITextView
    // Need to add code to adjust for this.
    CGRect attachmentRect = [self frameOfTextRange:range inTextView:textView];
      _attachmentMenuSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                             destructiveButtonTitle:nil
                                                  otherButtonTitles:@"Copy Image", @"Save to Camera Roll", @"Open in Viewer", nil];

    // Show the sheet
    [_attachmentMenuSheet showFromRect:attachmentRect inView:textView animated:YES];
}
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {

    CGRect rect = [textView.layoutManager boundingRectForGlyphRange:range inTextContainer:textView.textContainer];

    // Now convert to textView coordinates
    CGRect rectRange = [textView convertRect:rect fromView:textView.textInputView];

    // Now convert to contentView coordinates
    CGRect rectRangeSuper = [self.contentView convertRect:rectRange fromView:textView];

    // Get the textView frame
    CGRect rectView = textView.frame;

    // Find the intersection of the two (in the same coordinate space)
    CGRect rectIntersect = CGRectIntersection(rectRangeSuper, rectView);

    // If no intersection then that's weird !!
    if (CGRectIsNull(rectIntersect)) {
        return rectRange;
    }

    // Now convert the intersection rect back to textView coordinates
    CGRect rectRangeInt = [textView convertRect:rectIntersect fromView:self.contentView];

    return rectRangeInt;
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (actionSheet == _attachmentMenuSheet) {
        switch (buttonIndex) {

            case 0:
                [self copyImageToPasteBoard:[_attachment image]];
                break;

            case 1:
                [self saveToCameraRoll:[_attachment image]];
                break;

            case 2:
                [self browseImage:[_attachment image]];
                break;

            default:
                break;
        }
    }
}
- (void)saveToCameraRoll:(UIImage*)image {
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
- (void)copyImageToPasteBoard:(UIImage*)image {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSData *data = UIImagePNGRepresentation(image);
    [pasteboard setData:data forPasteboardType:@"public.png"];
}

-(void)browseImage:(UIImage*)image
{

    OSImageViewController *_imageViewerController = [[OSImageViewController alloc] init];
    UIImage *img = [[UIImage alloc] initWithData:UIImagePNGRepresentation(image)];

    _imageViewerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imageViewerController.modalPresentationStyle = UIModalPresentationFullScreen;
    _imageViewerController.delegate = self;
    [self presentViewController:_imageViewerController animated:YES completion:^(void){
        [_imageViewerController setImage:img];

    }];

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将自定义菜单项添加到UITextView菜单,UITextView菜单是指向所选单词的Wikipedia页面的链接?

来自分类Dev

如何将选项选项卡式菜单添加到大型下拉菜单?

来自分类Dev

如何将文件上传添加到电子表格作为自定义菜单选项?

来自分类Dev

将菜单选项添加到浮动操作按钮

来自分类Dev

如何将子菜单添加到导航菜单

来自分类Dev

如何将“多级菜单添加到汉堡菜单”

来自分类Dev

如何将 JSON 数组添加到无限数量的选项菜单中?

来自分类Dev

如何将Matlab添加到主菜单?

来自分类Dev

如何将glyphicon添加到spring struts菜单

来自分类Dev

如何将项目添加到Xfce根菜单?

来自分类Dev

如何将onclick事件添加到Wordpress菜单

来自分类Dev

如何将Rhythmbox控件添加到声音菜单?

来自分类Dev

如何将Rhythmbox控件添加到声音菜单?

来自分类Dev

如何将项目添加到Xfce根菜单?

来自分类Dev

如何将Matlab添加到主菜单?

来自分类Dev

Lubuntu-如何将软件添加到菜单

来自分类Dev

如何将弹出菜单添加到自定义listview到每个项目的android

来自分类Dev

如何将“新建文档”选项添加到桌面上下文菜单?

来自分类Dev

Polymer 1.0将选项动态添加到菜单

来自分类Dev

如何将自定义菜单项添加到UITextView菜单,该菜单项是到所选单词的Wikipedia页面的链接?

来自分类Dev

将弹出子菜单项添加到子菜单

来自分类Dev

如何在wxPython中将子菜单项添加到弹出菜单项?

来自分类Dev

如何将下拉菜单项添加到我的水平菜单栏中?

来自分类Dev

如何将图像添加到primefaces菜单栏?

来自分类Dev

Wordpress:如何将url GET参数添加到主菜单项

来自分类Dev

如何将最近打开的文档添加到WPF中的OpenRecent菜单项

来自分类Dev

如何将列表项动态添加到Foundation手风琴菜单

来自分类Dev

如何将活动班级添加到单击菜单项上的现有班级?

来自分类Dev

如何将子菜单添加到WordPress管理栏

Related 相关文章

  1. 1

    如何将自定义菜单项添加到UITextView菜单,UITextView菜单是指向所选单词的Wikipedia页面的链接?

  2. 2

    如何将选项选项卡式菜单添加到大型下拉菜单?

  3. 3

    如何将文件上传添加到电子表格作为自定义菜单选项?

  4. 4

    将菜单选项添加到浮动操作按钮

  5. 5

    如何将子菜单添加到导航菜单

  6. 6

    如何将“多级菜单添加到汉堡菜单”

  7. 7

    如何将 JSON 数组添加到无限数量的选项菜单中?

  8. 8

    如何将Matlab添加到主菜单?

  9. 9

    如何将glyphicon添加到spring struts菜单

  10. 10

    如何将项目添加到Xfce根菜单?

  11. 11

    如何将onclick事件添加到Wordpress菜单

  12. 12

    如何将Rhythmbox控件添加到声音菜单?

  13. 13

    如何将Rhythmbox控件添加到声音菜单?

  14. 14

    如何将项目添加到Xfce根菜单?

  15. 15

    如何将Matlab添加到主菜单?

  16. 16

    Lubuntu-如何将软件添加到菜单

  17. 17

    如何将弹出菜单添加到自定义listview到每个项目的android

  18. 18

    如何将“新建文档”选项添加到桌面上下文菜单?

  19. 19

    Polymer 1.0将选项动态添加到菜单

  20. 20

    如何将自定义菜单项添加到UITextView菜单,该菜单项是到所选单词的Wikipedia页面的链接?

  21. 21

    将弹出子菜单项添加到子菜单

  22. 22

    如何在wxPython中将子菜单项添加到弹出菜单项?

  23. 23

    如何将下拉菜单项添加到我的水平菜单栏中?

  24. 24

    如何将图像添加到primefaces菜单栏?

  25. 25

    Wordpress:如何将url GET参数添加到主菜单项

  26. 26

    如何将最近打开的文档添加到WPF中的OpenRecent菜单项

  27. 27

    如何将列表项动态添加到Foundation手风琴菜单

  28. 28

    如何将活动班级添加到单击菜单项上的现有班级?

  29. 29

    如何将子菜单添加到WordPress管理栏

热门标签

归档