How to get rid of "Highlight" context menu item in PDFView?
Asked Answered
D

4

8

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.

Doig answered 6/3, 2023 at 3:34 Comment(2)
Hi, Phil. I tried to hack with the internal private subviews, could you please take a look at github.com/kasimok/75646856Indies
Have you tryied to override the "LongPressureGesture" to nothing?Repeal
B
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: [])
        }
    }
}
Bogor answered 6/6, 2023 at 8:11 Comment(1)
Hi, Mike, the Highlight gone, but can't make selection when long press if i create a empty UIMenu.Doig
G
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)")
        }
    }
}
Gaunt answered 3/7, 2023 at 6:24 Comment(0)
B
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: [])
        }
    }
}
Bogor answered 6/6, 2023 at 8:11 Comment(1)
Hi, Mike, the Highlight gone, but can't make selection when long press if i create a empty UIMenu.Doig
R
0

Apple prevents the appearance of 2 UIMenus simultaneously. I managed to get rid of the highlight option by showing my custom UIMenu right after the default one.

import UIKit
import PDFKit

final class MSPDFView: PDFView, UIEditMenuInteractionDelegate {
    private var customMenu: UIMenu?
    private var interaction: UIEditMenuInteraction?
    private var canShowMenu = true
    
    private var configuration: UIEditMenuConfiguration? {
        if let selection = currentSelection,
           let currentPage = currentPage {
            let bounds = selection.bounds(for: currentPage)
            let point = convert(CGPoint(x: bounds.midX, y: bounds.maxY), from: currentPage)
            let configuration = UIEditMenuConfiguration(identifier: "MSMenu", sourcePoint: point)
            return configuration
        }
        return nil
    }
    
    init() {
        super.init(frame: .zero)
        autoScales = true
        setValue(true, forKey: "forcesTopAlignment")
        let interaction = UIEditMenuInteraction(delegate: self)
        self.interaction = interaction
        addInteraction(interaction)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func buildMenu(with builder: UIMenuBuilder) {
        if canShowMenu, let configuration = self.configuration {
            canShowMenu = false
            customMenu = builder.menu(for: .standardEdit)
            DispatchQueue.main.async { [weak self] in
                self?.showMenu(for: configuration)
            }
        }
    }
    
    private func showMenu(for configuration: UIEditMenuConfiguration) {
        interaction?.presentEditMenu(with: configuration)
        canShowMenu = true
    }
    
    func editMenuInteraction(_ interaction: UIEditMenuInteraction, menuFor configuration: UIEditMenuConfiguration, suggestedActions: [UIMenuElement]) -> UIMenu? {
        customMenu
    }
}
Rhythmandblues answered 23/3, 2024 at 13:47 Comment(0)
T
-6

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!

Transferase answered 5/6, 2023 at 8:44 Comment(2)
I'm sorry, Alperk, but where is editMenuInteraction property in pdfView?Indies
Looks like ChatGPTIdyllist

© 2022 - 2025 — McMap. All rights reserved.