如何快速使用PdfKit为链接注释添加颜色或添加操作以突出显示注释

时间:2019-03-07 10:36:30

标签: xcode10 ios12 swift4.2 ios-pdfkit

我的应用程序中有一些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的结果。 enter image description here

2 个答案:

答案 0 :(得分:1)

尽管这是一个hack,但这也不是一个适当的解决方案。

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

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

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

page?.addAnnotation(linkAnnotation)

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

答案 1 :(得分:0)

您不需要向注释添加任何操作,您可以通过添加通知观察免费获得它,因为 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!)
    }
}