Swift PDFKit 亮点

问题描述 投票:0回答:1

如何使用 PDFKit 与突出显示按钮交互?当我按“突出显示”时,无法突出显示所选文本。

struct PDFTestView: UIViewRepresentable {
    let document: PDFDocument
    
    func makeUIView(context: Context) -> PDFView {
        let pdfView = PDFView()
        pdfView.document = document
        pdfView.scaleFactor = 0.55
        pdfView.autoScales = true
        
        return pdfView
    }
    
    func updateUIView(_ uiView: PDFView, context: Context) {
            addHighlight(view: uiView)
    }
    
    func addHighlight(view: PDFView) {
        guard let selections = view.currentSelection?.selectionsByLine() else { return }
        
        selections.forEach { selection in
            selection.pages.forEach { page in
                let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
                highlight.color = .yellow
                page.addAnnotation(highlight)
            }
        }
    }
}

如果我添加一个按钮将 bool 属性切换为 true,我只能突出显示所选文本。

struct PDFTestView: UIViewRepresentable {
    let document: PDFDocument
    @Binding var isHighlight: Bool

    func makeUIView(context: Context) -> PDFView {
        let pdfView = PDFView()
        pdfView.document = document
        pdfView.scaleFactor = 0.55
        pdfView.autoScales = true

        return pdfView
    }

    func updateUIView(_ uiView: PDFView, context: Context) {
        if isHighlight { // <--- isHighlight toggled
            addHighlight(view: uiView)
        }
    }

    func addHighlight(view: PDFView) {
        guard let selections = view.currentSelection?.selectionsByLine() else { return }

        selections.forEach { selection in
            selection.pages.forEach { page in
                let highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
                highlight.color = .yellow
                page.addAnnotation(highlight)
            }
        }
    }
}

任何帮助将不胜感激。

swift highlight pdfkit
1个回答
0
投票

我也有同样的问题。你是如何解决这个问题的?任何帮助将不胜感激。

© www.soinside.com 2019 - 2024. All rights reserved.