1

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

4 回答 4

6

好的,我自己想通了,在这里阅读这个帖子:https ://forums.developer.apple.com/thread/93414

似乎highlightedSelections不像记录的那样工作。我将向 Apple 提交错误。

PDFSelection本身有一个 Type 的内部数组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-02-22T12:27:26.040 回答
3

扩展 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-11-15T20:15:36.693 回答
3

对我有用的唯一解决方案是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)
})
于 2018-02-22T11:38:54.490 回答
1

您需要将实例添加到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)
}
于 2020-01-15T18:26:02.077 回答