I am trying to include a highlighting function within a pdfViewer. However, in order to add the highlight annotation, I need the bounds for the selected text as a CGRect. Is there any way I can obtain this?
let annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil)
Get an array of selections where each selection corresponds to a single line of the selected text:
guard let selections = pdfView.currentSelection?.selectionsByLine()
else { return }
Loop over the selections line by line, then loop over the pages encompassed by each selection, Then create a new highlight annotation with the selection's bounds and add it to the page
selections.forEach({ selection in
selection.pages.forEach({ page in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.color = .yellow
page.addAnnotation(highlight)
})
})