6

I'm using PDFKit to render a PDF and I have added custom menus using "UIMenuController". But it is now deprecated from iOS 16 onwards.

I remove share、lookup menu items with code below:

@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)
}

But the "Highlight" context menu can't be removed when a user long press to select text in a PDF. There is no way to get rid of this menu item ? And how to use UIEditMenuInteraction in PDFView ?

Any help would be really appreciated.

kakaiikaka
  • 3,530
  • 13
  • 19
Phil
  • 201
  • 1
  • 7

3 Answers3

2

check this answer from this link

Removing highlight annotation with PDFKit

also you can use this library

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)")
        }
    }
}
Chandaboy
  • 1,302
  • 5
  • 10
1

Use the UIContextMenuInteraction that's available since iOS 13:

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 D.
  • 116
  • 6
  • Hi, Mike, the Highlight gone, but can't make selection when long press if i create a empty UIMenu. – Phil Jun 13 '23 at 09:18
-4

The "Highlight" context menu item is part of the default UIEditMenuInteraction, which is now the preferred way to handle text selection and editing in PDFView. To remove the "Highlight" menu item, you can override the editMenuInteraction property of your PDFView and set its menuItems property to an empty array.

Here is an example of how to do this:

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 = []
    }
}

This will remove the "Highlight" menu item from the context menu that appears when you long press to select text in a PDF.

You can also use the UIEditMenuInteraction to add custom menu items to the context menu. To do this, you can add items to the menuItems property of the UIEditMenuInteraction.

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

I hope this helps!

Alperk
  • 106
  • 4