【问题标题】:Wrong highlight annotation on apple PDFKit苹果PDFKit上的错误高亮注释
【发布时间】:2017-11-24 08:51:56
【问题描述】:

我在 iOS 上使用 PDFKit 来突出显示文本(PDF 文件)。我通过创建 PDFAnnotation 并将其添加到选定的文本区域来做到这一点。我想精确地突出显示选定的区域,但它总是覆盖整条线,如下图所示。如何只为选定区域创建注释??

我的代码:

        let highlight = PDFAnnotation(bounds: selectionText.bounds(for: page), forType: PDFAnnotationSubtype.highlight, withProperties: nil)
        highlight.color = highlightColor
        page.addAnnotation(highlight)

【问题讨论】:

    标签: ios swift pdf pdfkit


    【解决方案1】:

    按照PDFKit Highlight Annotation: quadrilateralPoints 中的建议,您可以使用quadrilateralPoints 为同一注释添加不同的行高亮显示。

    func highlight() {  
        guard let selection = pdfView.currentSelection, let currentPage = pdfView.currentPage else {return}
        let selectionBounds = selection.bounds(for: currentPage)
        let lineSelections = selection.selectionsByLine()
    
        let highlightAnnotation = PDFAnnotation(bounds: selectionBounds, forType: PDFAnnotationSubtype.highlight, withProperties: nil)
    
        highlightAnnotation.quadrilateralPoints = [NSValue]()
        for (index, lineSelection) in lineSelections.enumerated() {
            let n = index * 4
            let bounds = lineSelection.bounds(for: pdfView.currentPage!)
            let convertedBounds = bounds.convert(to: selectionBounds.origin)
            highlightAnnotation.quadrilateralPoints?.insert(NSValue(cgPoint: convertedBounds.topLeft), at: 0 + n)
            highlightAnnotation.quadrilateralPoints?.insert(NSValue(cgPoint: convertedBounds.topRight), at: 1 + n)
            highlightAnnotation.quadrilateralPoints?.insert(NSValue(cgPoint: convertedBounds.bottomLeft), at: 2 + n)
            highlightAnnotation.quadrilateralPoints?.insert(NSValue(cgPoint: convertedBounds.bottomRight), at: 3 + n)
        }
    
        pdfView.currentPage?.addAnnotation(highlightAnnotation)
    }
    
    extension CGRect {
    
        var topLeft: CGPoint {
            get {
                return CGPoint(x: self.origin.x, y: self.origin.y + self.height)
            }
        }
    
        var topRight: CGPoint {
            get {
                return CGPoint(x: self.origin.x + self.width, y: self.origin.y + self.height)
            }
        }
    
        var bottomLeft: CGPoint {
            get {
                return CGPoint(x: self.origin.x, y: self.origin.y)
            }
        }
    
        func convert(to origin: CGPoint) -> CGRect {
            return CGRect(x: self.origin.x - origin.x, y: self.origin.y - origin.y, width: self.width, height: self.height)
        }
    }
    

    【讨论】:

    • 感谢quadrilateralPoints,我不知道。顺便说一句,扩展中缺少 bottomRight。
    【解决方案2】:

    PDFSelection bounds(forPage:) 方法返回一个矩形来满足整个选择区域。不是您的最佳解决方案。

    尝试使用selectionsByLine(),并为每个矩形添加单独的注释,代表 PDF 中的每个选定行。示例:

        let selections = pdfView.currentSelection?.selectionsByLine()
        // 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)
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 2020-02-25
      • 2018-04-06
      • 2015-04-13
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多