【问题标题】:How to colour link annotation or add action to highlight annotation using PdfKit in swift如何在 swift 中使用 PdfKit 为链接注释着色或添加动作以突出显示注释
【发布时间】:2019-07-29 05:11:28
【问题描述】:

我的应用程序中有一些 pdf,并且正在快速使用 PdfKit。现在我想突出显示 pdf 文档中的所有超链接。我尝试使用 PDFAnnotationSubtype.highlight 和 PDFAnnotationSubtype.link 但在这两种情况下我都无法实现我的目标。

对于 PDFAnnotationSubtype.highlight - 点击链接时我无法对其添加操作。

对于 PDFAnnotationSubtype.link - 我无法为链接设置颜色或背景颜色。

这是我的代码,如果这里遗漏了什么,请纠正我。

//here i get my pdf from document directory
let filePAth = (self.getDirectoryPath() as NSString).appendingPathComponent(webURLString)
    let fileURL = URL(fileURLWithPath: filePAth)

    let document = PDFDocument(url: fileURL)

    self.pdfView.displayMode = .twoUpContinuous
    self.pdfView.displayDirection = .horizontal
     pdfView.usePageViewController(true, withViewOptions: [UIPageViewController.OptionsKey.interPageSpacing: 0])

    //here is the hack 
    //i am getting annotation  
   let count = document?.pageCount

    for i in 0 ..< count! {

        let page = document?.page(at: i)
        let annotationArray = page1?.annotations

      for annotationObj in annotationArray! {
            if annotationObj.type! == "Link" {

                //case 1: highlights hyperlinks links but
                //added action does not work
                let annotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.highlight, withProperties: nil)

                annotation.color = UIColor.red
                annotation.isHighlighted = true

                // highlights all the hyperlinks with red color
                // but added action does not work here

                let url = URL(string: "http://google.com/")
                annotation.action = PDFActionURL(url: url!)

               page?.addAnnotation(annotation)

             //case 2: does not highlights hyperlinks links with red 
             //color but added action works

                let annotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
                // no effect 
                annotation.color = UIColor.red
                annotation.isHighlighted = true

                // does not highlight all the hyperlinks with red 
                // but added action works here

                let url = URL(string: "http://google.com/")
                annotation.action = PDFActionURL(url: url!)

               page?.addAnnotation(annotation)

            }
        }
    }

我能找到路。任何人都可以提出一些建议吗? 图像是代码中提到的案例 1 的结果。

【问题讨论】:

    标签: xcode10 ios12 swift4.2 ios-pdfkit


    【解决方案1】:

    您不需要为注释添加任何操作,您可以通过添加通知观察免费获得它,因为当单击注释时,PDFKit 会发布名为“PDFViewAnnotationHit”的通知。 Here is Apple's documentation

    func addAnnotationNotification() {
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(annotationHit(notification:)),
            name: Notification.Name.PDFViewAnnotationHit, object: nil
        )
    }
    
    @objc func annotationHit(notification: Notification) {
        if let annotation = notification.userInfo?["PDFAnnotationHit"] as? PDFAnnotation {
            print(annotation.bounds)
            print(annotation.contents!)
        }
    }
    

    【讨论】:

      【解决方案2】:

      即使它不是一个适当的解决方案,尽管它是一个 hack。

      第 1 步 - 创建链接注释并将现有注释操作添加到新链接注释。

      let linkAnnotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
      linkAnnotation.action = obj.annotation
      

      第 2 步 - 在突出显示的注释之后向页面添加新的链接注释。

      page?.addAnnotation(linkAnnotation)
      

      尝试让我知道这是否适合您。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 2012-12-30
        相关资源
        最近更新 更多