I've set up a search bar in my pdf view and can highlight the words I'm looking for. When I try to remove the highlights so that I can search for new words. I've tried to reverse what I did, change the color to white, and remove annotations yet nothing is working.I'm wanting to clear the highlights when the user click the cancel button. What am I doing wrong?
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
i = 0
unhighlight(searchTerms: [searchWord])
searchBar.text! = ""
}
private func highlight(searchTerms: [String]?)
{
searchTerms?.forEach { term in
selections = pdfView.document?.findString(term, withOptions: [.caseInsensitive])
selections?.forEach { selection in
selection.pages.forEach { page in
highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
page.addAnnotation(highlight)
}
}
}
}
func unhighlight(searchTerms: [String]?)
{
searchTerms?.forEach { term in
selections = pdfView.document?.findString(term, withOptions: [.caseInsensitive])
selections?.forEach { selection in
selection.pages.forEach { page in
highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.white.withAlphaComponent(0.5)
page.addAnnotation(highlight)
}
}
}
}