如何在PDFView中去除“高亮显示”上下文菜单项?

7

我正在使用PDFKit来渲染PDF,并使用“UIMenuController”添加自定义菜单。但是从iOS 16开始,它已被弃用。

我使用以下代码删除了分享和查找菜单项:

@available(iOS 13.0, *)
open override func buildMenu(with builder: UIMenuBuilder) {
    builder.remove(menu: .lookup)
    builder.remove(menu: .share)
    builder.remove(menu: .replace)
    super.buildMenu(with: builder)
}

但是当用户长按选择PDF中的文本时,“高亮”上下文菜单无法删除。没有办法摆脱这个菜单项吗?如何在PDFView中使用UIEditMenuInteraction?

非常感谢任何帮助。


嗨,Phil。我试图通过内部私有子视图进行黑客攻击,请你看一下https://github.com/kasimok/75646856。 - kakaiikaka
你尝试过将"LongPressureGesture"设置为空吗? - Allan Garcia
3个回答

2
检查这个链接中的答案 使用PDFKit删除高亮注释 你还可以使用这个库

https://pspdfkit.com/guides/ios/features/controlling-pdf-editing/

 //Remove highlight annotations that are part of the current selection.
@objc func removeHighlightSelection(_ sender: UIMenuItem) {
    let selection = PDFSelection.init(document: pdfViewer.document!)
    selection.add(pdfViewer.currentSelection!)
    let selectionBounds = selection.bounds(for: pdfViewer.currentPage!)
    let annotations = pdfViewer.currentPage?.annotations
    let annotationsToDelete = NSMutableArray()

    for annotation in annotations! {
        let annotationPoint = annotation.bounds.origin
        if selectionBounds.contains(annotationPoint) {
            annotationsToDelete.add(annotation)
            print(annotationsToDelete)
        }
    }

    for annotation in annotationsToDelete {
        let onlyHighlight = annotation as! PDFAnnotation
        if onlyHighlight.type == "Highlight" {
            pdfViewer.currentPage?.removeAnnotation(onlyHighlight)
            print("Removed highlight annotation: \(onlyHighlight)")
        }
    }
}

1
使用自iOS 13起可用的UIContextMenuInteraction
import PDFKit

class ViewController: UIViewController, UIContextMenuInteractionDelegate {
    // ...

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set up the UIContextMenuInteraction for your PDFView
        let interaction = UIContextMenuInteraction(delegate: self)
        pdfView.addInteraction(interaction)
    }

    // Implement the UIContextMenuInteractionDelegate method
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
            // Create an empty UIMenu
            return UIMenu(title: "", children: [])
        }
    }
}

嗨,Mike,突出显示消失了,但是如果我创建一个空的UIMenu,长按时无法进行选择。 - Phil
嗨,Mike,突出显示已消失,但是如果我创建一个空的UIMenu,长按时无法进行选择。 - undefined

-4
“Highlight”上下文菜单项是默认的UIEditMenuInteraction的一部分,现在已成为处理PDFView中文本选择和编辑的首选方式。要移除“Highlight”菜单项,您可以覆盖PDFView的editMenuInteraction属性,并将其menuItems属性设置为空数组。
以下是如何执行此操作的示例:
class MyViewController: UIViewController {

    @IBOutlet weak var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Remove the "Highlight" menu item from the default UIEditMenuInteraction
        pdfView.editMenuInteraction?.menuItems = []
    }
}

这将从长按选择PDF文本时出现的上下文菜单中移除“高亮”菜单项。
您还可以使用UIEditMenuInteraction向上下文菜单添加自定义菜单项。要做到这一点,您可以将项目添加到UIEditMenuInteraction的menuItems属性中。
class MyViewController: UIViewController {

    @IBOutlet weak var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create a custom menu item
        let myMenuItem = UIMenuItem(title: "My Item", action: #selector(myMenuItemTapped))

        // Add the custom menu item to the default UIEditMenuInteraction
        pdfView.editMenuInteraction?.menuItems.append(myMenuItem)
    }

    @objc func myMenuItemTapped() {
        // Do something when the custom menu item is tapped
    }
}

希望这能帮到你!

3
抱歉,Alperk,pdfView中的editMenuInteraction属性在哪里? - kakaiikaka
3
对不起,Alperk,pdfView中的editMenuInteraction属性在哪里? - undefined
2
看起来像是ChatGPT。 - DavidW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接