PDFView 不突出显示搜索结果
PDFView doesn't highlight search results
我正在试用 PDFView 并想突出显示搜索到的词。
我正在关注这个小 tutorial right here(请参阅搜索段落)。
我正在为我的 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")
}
}
唯一对我有用的解决方案是 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://forums.developer.apple.com/thread/93414
highlightedSelections
似乎没有按照记录工作。我将向 Apple 提交错误。
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)
}
}
}
扩展 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)
}
}
}
}
您需要将实例添加到 highlightedSelections
func viewDidLoad() {
super.viewDidLoad()
//load your pdf ...
//...
self.youPdfView?.document.delegate = self
self.yourPdfView?.highlightedSelections = [PDFSelection]()
self.yourPdfView?.document?.beginFindString("foo", withOptions: .caseInsensitive)
}
func didMatchString(_ instance: PDFSelection) {
instance.color = .yellow
self.pdfView?.highlightedSelections?.append(instance)
}
我正在试用 PDFView 并想突出显示搜索到的词。 我正在关注这个小 tutorial right here(请参阅搜索段落)。
我正在为我的 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")
}
}
唯一对我有用的解决方案是 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://forums.developer.apple.com/thread/93414
highlightedSelections
似乎没有按照记录工作。我将向 Apple 提交错误。
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)
}
}
}
扩展 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)
}
}
}
}
您需要将实例添加到 highlightedSelections
func viewDidLoad() {
super.viewDidLoad()
//load your pdf ...
//...
self.youPdfView?.document.delegate = self
self.yourPdfView?.highlightedSelections = [PDFSelection]()
self.yourPdfView?.document?.beginFindString("foo", withOptions: .caseInsensitive)
}
func didMatchString(_ instance: PDFSelection) {
instance.color = .yellow
self.pdfView?.highlightedSelections?.append(instance)
}