Skip to content

自定义菜单

在查看 PDF 时,选择文本并在空白区域长按后,PDF 上下文将进入相应的交互状态。在不同的交互状态下,会弹出类似于UIMenuController的上下文菜单,您可以通过上下文菜单选项执行相关操作。

以下是设置自定义菜单的示例代码:

swift
func menuItems(at point: CGPoint, for page: CPDFPage) -> [UIMenuItem] {
    var items = super.menuItems(at: point, for: page) ?? []

    var menuItems = [UIMenuItem]()
    
    if let currentSelection = self.currentSelection {
        let textNoteItem = UIMenuItem(title: NSLocalizedString("Note", comment: ""),
                                      action: #selector(textNoteItemAction(_:)))
        let textShareItem = UIMenuItem(title: NSLocalizedString("Share", comment: ""),
                                       action: #selector(textShareItemAction(_:)))
        let defineItem = UIMenuItem(title: NSLocalizedString("Define", comment: ""),
                                    action: #selector(defineItemAction(_:)))
        let linkItem = UIMenuItem(title: NSLocalizedString("Link", comment: ""),
                                  action: #selector(linkItemAction(_:)))
        let searchItem = UIMenuItem(title: NSLocalizedString("Search", comment: ""),
                                    action: #selector(searchItemAction(_:)))
        
        menuItems.append(textNoteItem)
        menuItems += [textShareItem, defineItem, linkItem, searchItem]
    } else {
        let textNoteItem = UIMenuItem(title: NSLocalizedString("Note", comment: ""),
                                      action: #selector(textNoteItemAction(_:)))
        let textItem = UIMenuItem(title: NSLocalizedString("Text", comment: ""),
                                  action: #selector(textItemAction(_:)))
        let pasteItem = UIMenuItem(title: NSLocalizedString("Paste", comment: ""),
                                   action: #selector(pasteItemAction(_:)))
        
        menuItems += [textNoteItem, textItem]
        
        let textType = kUTTypeText as String
        let urlType = kUTTypeURL as String
        let urlFileType = kUTTypeFileURL as String
        let jpegImageType = kUTTypeJPEG as String
        let pngImageType = kUTTypePNG as String
        let rawImageType = "com.apple.uikit.image"
        
        let isPasteboardValid = UIPasteboard.general.contains(pasteboardTypes: [textType, urlType, urlFileType, jpegImageType, pngImageType, rawImageType])
        if isPasteboardValid {
            menuItems.append(pasteItem)
        }
    }
    
    return menuItems + items
}
objective-c
- (NSArray<UIMenuItem *> *)menuItemsAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
    NSArray *items = [super menuItemsAtPoint:point forPage:page];
    
    NSMutableArray *menuItems = [NSMutableArray arrayWithArray:items];
    if (self.currentSelection) {
        UIMenuItem *textNoteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", nil)
                                                              action:@selector(textNoteItemAction:)];
        UIMenuItem *textShareItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Share", nil)
                                                               action:@selector(textShareItemAction:)];
        UIMenuItem *defineItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Define", nil)
                                                            action:@selector(defineItemAction:)];
        UIMenuItem *linkItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Link", nil)
                                                          action:@selector(linkItemAction:)];
        UIMenuItem *searchItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Search", nil)
                                                            action:@selector(searchItemAction:)];
        [menuItems insertObject:textNoteItem atIndex:0];
        [menuItems addObject:textShareItem];
        [menuItems addObject:defineItem];
        [menuItems addObject:linkItem];
        [menuItems addObject:searchItem];
    } else {
        UIMenuItem *textNoteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", nil)
                                                              action:@selector(textNoteItemAction:)];
        UIMenuItem *textItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Text", nil)
                                                          action:@selector(textItemAction:)];
        UIMenuItem *pasteItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Paste", nil)
                                                           action:@selector(pasteItemAction:)];
        [menuItems addObject:textNoteItem];
        [menuItems addObject:textItem];
        
        NSString *textType = (NSString *)kUTTypeText;
        NSString *urlType  = (NSString*)kUTTypeURL;
        NSString *urlFileType  = (NSString*)kUTTypeFileURL;
        NSString *jpegImageType = (NSString *)kUTTypeJPEG;
        NSString *pngImageType = (NSString *)kUTTypePNG;
        NSString *rawImageType = @"com.apple.uikit.image";
        BOOL isPasteboardValid = [[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:textType, urlType, urlFileType, jpegImageType, pngImageType, rawImageType, nil]];
        if (isPasteboardValid) {
            [menuItems addObject:pasteItem];
        }
    }
    return menuItems;
}