PDFView不会突出显示搜索结果

时间:2018-02-22 10:25:27

标签: ios swift pdfkit pdfview

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

4 个答案:

答案 0 :(得分:4)

好的,我通过在这里阅读这个帖子来解决这个问题: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)
        }
    }
}

答案 1 :(得分:2)

对我有用的唯一解决方案是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)
})

答案 2 :(得分:0)

通过扩展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)
            }
         }
      }
   }

答案 3 :(得分:0)

您需要将实例添加到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)
}