如何使用PDFKit突出显示pdf中的选定文本?

时间:2019-07-02 22:23:56

标签: ios swift highlight ios-pdfkit apple-pdfkit

我已经设置了PDFViewer,并希望添加突出显示功能,以便用户选择文本时可以突出显示它。当您在便笺,iMessages等中突出显示文本时,您可以选择全部,复制,粘贴等内容。如何进行编辑,以便也可以具有突出显示功能?此外,应用程序将如何保存突出显示,以便当用户关闭并重新打开应用程序时,他们仍然能够查看突出显示的文本?这会涉及使用核心数据还是其他?谢谢!

this is a screenshot of the default functionalities that Apple provides but I would like to add an additional highlighting functionality

1 个答案:

答案 0 :(得分:0)

let select = pdfView.currentSelection?.selectionsByLine()
    //assuming for single-page pdf.
    guard let page = select?.first?.pages.first else { return }

    select?.forEach({ selection in
        let highlight = PDFAnnotation(bounds: select.bounds(for: page), forType: .highlight, withProperties: nil)
        highlight.endLineStyle = .square
        highlight.color = UIColor.orange.withAlphaComponent(0.5)

        page.addAnnotation(highlight)
    })