gpt4 book ai didi

ios - PDFView 不突出显示搜索结果

转载 作者:行者123 更新时间:2023-11-28 09:32:05 25 4
gpt4 key购买 nike

我正在试用 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")
}
}

最佳答案

好吧,我自己想通了,通过阅读这里的这个线程: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)
}
}
}

关于ios - PDFView 不突出显示搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com