Skip to content

Instantly share code, notes, and snippets.

@derickito
Last active October 29, 2024 04:32
Show Gist options
  • Save derickito/ac332db155ffbf0cff712d1680bd9fc0 to your computer and use it in GitHub Desktop.
Save derickito/ac332db155ffbf0cff712d1680bd9fc0 to your computer and use it in GitHub Desktop.

Revisions

  1. derickito revised this gist Oct 17, 2018. No changes.
  2. derickito created this gist Oct 17, 2018.
    22 changes: 22 additions & 0 deletions PDFViewController.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    override func viewDidLoad() {
    createMenu()
    }

    private func createMenu() {
    let highlightItem = UIMenuItem(title: "Highlight", action: #selector(highlight(_:)))
    UIMenuController.shared.menuItems = [highlightItem]
    }

    @objc private func highlight(_ sender: UIMenuController?) {
    guard let currentSelection = pdfView.currentSelection else { return }
    let selections = currentSelection.selectionsByLine()
    guard let page = selections.first?.pages.first else { return }

    selections.forEach { selection in
    let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
    highlight.endLineStyle = .square
    page.addAnnotation(highlight)
    }

    pdfView.clearSelection()
    }