我正在试用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")
}
}发布于 2018-02-22 12:27:26
好吧,我自己看了这篇文章:https://forums.developer.apple.com/thread/93414
highlightedSelections似乎不像文档所描述的那样工作。我会在苹果公司注册一个窃听器。
PDFSelection本身有一个PDFSelection类型的内部数组。有了这个,我可以在其中添加多个选择。之后,我可以使用pdfView.setCurrentSelection(_:animate:)来设置这个嵌套的选择数组。
var document: PDFDocument!
var searchedItem: 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)
self.document?.beginFindString("Product", withOptions: .caseInsensitive)
}
func documentDidEndDocumentFind(_ notification: Notification) {
pdfView.setCurrentSelection(searchedItem, animate: true)
}
func documentDidFindMatch(_ notification: Notification) {
if let selection = notification.userInfo?.first?.value as? PDFSelection {
selection.color = .yellow
if searchedItem == nil {
// The first found item sets the object.
searchedItem = selection
} else {
// All other found selection will be nested
searchedItem!.add(selection)
}
}
}发布于 2018-11-15 20:15:36
扩展到Rafal的答案,我让它工作,即使多页文档(和多个搜索词),就像这样。我还不能测试的是,这是否适用于跨越多个页面的术语:
private func highlight(searchTerms: [String]?)
{
searchTerms?.forEach { term in
let selections = pdfView?.document?.findString(term, withOptions: [.caseInsensitive])
selections?.forEach { selection in
selection.pages.forEach { page in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
page.addAnnotation(highlight)
}
}
}
}发布于 2018-02-22 11:38:54
唯一适用于我的解决方案是PDFAnnotation和.highlight类型:
let selections = pdfView?.document?.findString("PDFKit, my dear!", withOptions: [.caseInsensitive])
// Simple scenario, assuming your pdf is single-page.
guard let page = selections?.first?.pages.first else { return }
selections?.forEach({ selection in
let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
highlight.endLineStyle = .square
highlight.color = UIColor.orange.withAlphaComponent(0.5)
page.addAnnotation(highlight)
})https://stackoverflow.com/questions/48925001
复制相似问题