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

How to highlight selected text in pdf using PDFKit?

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

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

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)
    })