我正在尝试 PDFView 并想突出显示搜索到的单词。我在这里关注这个小教程(参见搜索段落)。
我正在为我的 PDF 获取匹配项,但是当我设置时pdfView.highlightedSelections = searchedItems
没有任何反应。
这是我的代码(VC 扩展了 PDFDocumentDelegate)
var document: PDFDocument!
var searchedItems: [PDFSelection] = []
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "test3", withExtension: "pdf")
document = PDFDocument(url: url!)
pdfView.document = document
pdfView.document?.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
pdfView.document?.findString("Product", withOptions: .caseInsensitive)
}
func documentDidEndDocumentFind(_ notification: Notification) {
pdfView.highlightedSelections = nil
pdfView.highlightedSelections = searchedItems
}
func documentDidFindMatch(_ notification: Notification) {
if let selection = notification.userInfo?.first?.value as? PDFSelection {
selection.color = .yellow
searchedItems.append(selection)
print("didFindMatch")
}
}