苹果PDFKit错误的高亮注释 [英] Wrong highlight annotation on apple PDFKit
问题描述
我在iOS上使用PDFKit来突出显示文本(PDF文件)。我通过创建PDFAnnotation并将其添加到选定的文本区域来实现。我想要精确地突出显示所选区域,但它总是覆盖整个线条,如下图所示。如何仅为所选区域创建注释?
I'm using PDFKit on iOS to highlight texts (PDF file). I do it by create a PDFAnnotation and add it to the selected text area. I want to highlight precisely the selected area but it always covers the whole line like the pics below. How can I create the annotation for the selected area only??
我的代码:
let highlight = PDFAnnotation(bounds: selectionText.bounds(for: page), forType: PDFAnnotationSubtype.highlight, withProperties: nil)
highlight.color = highlightColor
page.addAnnotation(highlight)
推荐答案
PDFSelection
bounds(forPage :)
方法返回一个矩形以满足整个选择区域。在你的情况下不是最好的解决方案。
PDFSelection
bounds(forPage:)
method returns one rectangle to satisfy whole selection area. Is not the best solution in your case.
尝试使用 selectionsByLine()
,并为每个rect添加单独的注释,表示PDF中的每个选定行。示例:
Try with selectionsByLine()
, and add individual annotation for every rect, representing every single selected line in PDF. Example:
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)
})
这篇关于苹果PDFKit错误的高亮注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!