4

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)



            }
        }
    }
}

1 Answers1

0

Not sure if you ever solved your issue, however i believe you simply pass in those annotations to the self.highlightedselections array built into PDFView. Setting that array to nil should clear out the highlights, instead of creating your own annotations.

On the search, it should provide a PDFSelection, in which you set the .color attribute

Jh2170
  • 116
  • 4