我在我的 pdf 视图中设置了一个搜索栏,可以突出显示我正在寻找的单词。当我尝试删除突出显示以便我可以搜索新单词时。我试图扭转我所做的,将颜色更改为白色,并删除注释但没有任何效果。我想在用户单击取消按钮时清除突出显示。我究竟做错了什么?
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)
}
}
}
}