首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFView没有突出显示搜索结果

PDFView没有突出显示搜索结果
EN

Stack Overflow用户
提问于 2018-02-22 10:25:26
回答 4查看 3.2K关注 0票数 2

我正在试用PDFView,并想突出显示搜索到的单词。我在跟踪这个小教程就在这里 (见搜索段落)。

我正在为我的PDF得到匹配,但是当我设置pdfView.highlightedSelections = searchedItems时,什么都不会发生。

这是我的代码(VC扩展了PDFDocumentDelegate)

代码语言:javascript
复制
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")
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-02-22 12:27:26

好吧,我自己看了这篇文章:https://forums.developer.apple.com/thread/93414

highlightedSelections似乎不像文档所描述的那样工作。我会在苹果公司注册一个窃听器。

PDFSelection本身有一个PDFSelection类型的内部数组。有了这个,我可以在其中添加多个选择。之后,我可以使用pdfView.setCurrentSelection(_:animate:)来设置这个嵌套的选择数组。

代码语言:javascript
复制
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)
        }
    }
}
票数 6
EN

Stack Overflow用户

发布于 2018-11-15 20:15:36

扩展到Rafal的答案,我让它工作,即使多页文档(和多个搜索词),就像这样。我还不能测试的是,这是否适用于跨越多个页面的术语:

代码语言:javascript
复制
   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)
            }
         }
      }
   }
票数 5
EN

Stack Overflow用户

发布于 2018-02-22 11:38:54

唯一适用于我的解决方案是PDFAnnotation.highlight类型:

代码语言:javascript
复制
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)
})
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48925001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档