【问题标题】:How to get the height of a paragraph using PDFKit如何使用 PDFKit 获取段落的高度
【发布时间】:2020-04-06 09:05:16
【问题描述】:

我正在使用 iOS PDFKit 编写 pdf。通常,我可以通过执行以下操作来获取单个文本项(例如标题)的高度:

return titleStringRect.origin.y + titleStringRect.size.height

其中 titleStringRect 是包含字符串的 CGRect。返回的值是该文本底部的 y 坐标,以便我知道从哪里开始编写下一行文本。 我还没有找到知道段落结束位置的方法。我发现的解决方案是制作一个足够大的 CGRect ,该段落肯定适合。 我需要确切地知道 CGRect 的高度应该基于将写入其中的字符串。这是我的代码:

    func addParagraph(pageRect: CGRect, textTop: CGFloat, text: String) {

        let textFont = UIFont(name: "Helvetica", size: 12)
        let backupFont = UIFont.systemFont(ofSize: 12, weight: .regular)

        // Set paragraph information. (wraps at word breaks)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .natural
        paragraphStyle.lineBreakMode = .byWordWrapping

        // Set the text attributes
        let textAttributes = [
            NSAttributedString.Key.paragraphStyle: paragraphStyle,
            NSAttributedString.Key.font: textFont ?? backupFont
        ]

        let attributedText = NSAttributedString(
            string: text,
            attributes: textAttributes
        )

        let textRect = CGRect(
            x: 50.0,
            y: textTop,
            width: pageRect.width - 100,
            height: pageRect.height - textTop - pageRect.height / 5.0
        )
        attributedText.draw(in: textRect)
    }

如您所见,上面的代码只是创建了一个 CGRect,它是前一个文本下方空间的 1/5,而不管段落实际有多少行。 我已经尝试平均每行的字符数,以估计该段落将有多少行,但这是不可靠的,绝对是一个 hack。 我需要 addParagraph 函数返回段落底部的 y 坐标,以便我知道从哪里开始编写下一段内容。

【问题讨论】:

    标签: ios swift ios-pdfkit nsmutableparagraphstyle


    【解决方案1】:

    我最终找到了解决方案,而且非常简单。我将发布代码,然后为遇到此问题的其他人解释它。

    let paragraphSize = CGSize(width: pageRect.width - 100, height: pageRect.height)
    let paragraphRect = attributedText.boundingRect(with: paragraphSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
    

    首先定义一个有一定宽高的CGSize。将宽度设置为您希望段落的宽度,并将高度设置为适合内容的较大值。然后调用

    attributedText.boundingRect(with: paragraphSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)

    AttributeText 是段落内容。 boundingRect 方法返回一个 CGRect ,它是适合内容所需的大小,但仅此而已。现在您可以返回段落的底部。此方法不会更改宽度,除非它无法将字符串放入您提供的高度。对我来说,这是完美的。完整代码如下:

     func addParagraph(pageRect: CGRect, textTop: CGFloat, paragraphText: String) -> CGFloat {
    
            let textFont = UIFont(name: "Helvetica", size: 12)
            let backupFont = UIFont.systemFont(ofSize: 12, weight: .regular)
    
            // Set paragraph information. (wraps at word breaks)
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .natural
            paragraphStyle.lineBreakMode = .byWordWrapping
    
            // Set the text attributes
            let textAttributes = [
                NSAttributedString.Key.paragraphStyle: paragraphStyle,
                NSAttributedString.Key.font: textFont ?? backupFont
            ]
    
            let attributedText = NSAttributedString(
                string: paragraphText,
                attributes: textAttributes
            )
    
            // determine the size of CGRect needed for the string that was given by caller
            let paragraphSize = CGSize(width: pageRect.width - 100, height: pageRect.height)
            let paragraphRect = attributedText.boundingRect(with: paragraphSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
    
            // Create a CGRect that is the same size as paragraphRect but positioned on the pdf where we want to draw the paragraph
            let positionedParagraphRect = CGRect(
                x: 50,
                y: textTop,
                width: paragraphRect.width,
                height: paragraphRect.height
            )
    
            // draw the paragraph into that CGRect
            attributedText.draw(in: positionedParagraphRect)
    
            // return the bottom of the paragraph
            return positionedParagraphRect.origin.y + positionedParagraphRect.size.height
        }
    
    

    【讨论】:

      猜你喜欢
      • 2013-04-05
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多