Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nguyenbathanh/f20662413f126f5fe36625a785cfa357 to your computer and use it in GitHub Desktop.
Save nguyenbathanh/f20662413f126f5fe36625a785cfa357 to your computer and use it in GitHub Desktop.
iOS PDFKit: How to add a highlight annotation
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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment