0

我正在使用 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 坐标,以便我知道从哪里开始编写下一段内容。

4

1 回答 1

2

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

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
    }

于 2019-12-13T04:03:09.443 回答