ios - PDFKit PDFSelection边界框坐标系

标签 ios swift xcode ios11

我想在 pdf 中找到一个特定的单词并在其上绘制一个边界框。 为此,我使用

func findString(_ string: String, 
withOptions options: NSString.CompareOptions = []) -> [PDFSelection]

PDFSelection 的坐标系是什么? 我想在 pdfview 的顶部绘制边界框(pdfselection 的属性)并在一个尴尬的地方获得矩形。

有人知道如何转换这个坐标吗?

这是我的代码

let searchResult = document.findString("VAT", withOptions: NSString.CompareOptions.caseInsensitive)

for result in searchResult {
    let box = result(for: document.page(at: 0)!)
    let convertedBox = pdf.convert(box, from: document.page(at: 0)!)
    let boxView = UIView(frame: convertedBox)
    boxView.backgroundColor = UIColor(red: 0.3, green: 0.6, blue: 0.1, alpha: 0.5)
    view.addSubview(boxView)
    print(box)
}

谢谢

最佳答案

PDFSelection 是页面中找到匹配项的位置

这个选择的边界是相对于页面本身的。

这是获取边界然后使用它添加注释的示例

let searchResult = document.findString("VAT", withOptions: .caseInsensitive)

for result in searchResult {

    let bounds = result.pages.map { result.bounds(for: $0)) }

    let annotations = bounds.map { PDFAnnotation(bounds: $0, forType: .highlight, withProperties: nil) }

    annotations.forEach { $0.page?.addAnnotation($0) }
}

请注意,如果文本很长并持续到下一页,则可能会在两页或更多页中找到匹配项

在上面的示例中,可以通过以下方式设置注释的颜色:PDFAnnotation.color 属性

PDFView 包含一个 UIScrollView,您可以通过此扩展获取它:

extension PDFView {
    var scrollView: UIScrollView? {
        return subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
    }
}

然后尝试计算实际框架,我添加前几页的大小,然后这应该是添加到 ScrollView 中的框架

关于ios - PDFKit PDFSelection边界框坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51273347/

相关文章:

iphone - 我在我的应用程序中使用的内存在终止后会变干净吗?甚至泄漏? (iOS)

ios - Swift - 使用 animateWithDuration 淡化 CAGradientLayer

swift - 如何为 Realm 构建查询对象

ios - 无法通过其标签更新 UISlider 值

Xcode : shortcut for highlight feature

ios - Objective-C 中的 "accessors"是什么?

ios CLLocation didUpdateLocations 错误

ios - 如何使用 AVPlayer 从数组中选择后播放下一首轨道

ios - IBAction 函数已正确连接但未执行

ios - uiimagePickerController didFinishPickingMediaWithInfo 在图像选择时没有被调用