Ios 如何使用PDFKit获取段落高度
我正在使用iOS PDFKit编写pdf。通常,我可以通过执行以下操作获得单个文本项(如标题)的高度:Ios 如何使用PDFKit获取段落高度,ios,swift,ios-pdfkit,nsmutableparagraphstyle,Ios,Swift,Ios Pdfkit,Nsmutableparagraphstyle,我正在使用iOS PDFKit编写pdf。通常,我可以通过执行以下操作获得单个文本项(如标题)的高度: return titleStringRect.origin.y + titleStringRect.size.height 其中titleStringRect是包含字符串的CGRect。返回的值是该文本底部的y坐标,以便我知道从何处开始写入下一行文本。 我还没有找到一种方法来知道一段的结尾。我找到的解决办法是只做一个足够大的CGRect,这段文字肯定会适合。 我需要确切地知道CGRect的高
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,而不管段落实际有多少行。
我尝试平均每行的字符数,以估计段落将有多少行,但这是不可靠的,肯定是一个黑客。
我需要的是addPagement函数返回段落底部的y坐标,以便我知道从何处开始编写下一段内容 我最终找到了解决这个问题的方法,而且非常简单。我将发布代码,然后向其他有此问题的人解释
let paragraphSize = CGSize(width: pageRect.width - 100, height: pageRect.height)
let paragraphRect = attributedText.boundingRect(with: paragraphSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
首先定义一个具有一定宽度和高度的CGSize。将宽度设置为您希望段落的宽度,并将高度设置为适合内容的较大值。然后打电话
AttributeText.boundingRect(带:段落大小,选项:NSStringDrawingOptions.usesLineFragmentOrigin,上下文:nil)
其中AttributeText是段落内容。boundingRect方法返回一个CGRect,该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
}
我最终找到了解决这个问题的方法,而且非常简单。我将发布代码,然后向其他有此问题的人解释
let paragraphSize = CGSize(width: pageRect.width - 100, height: pageRect.height)
let paragraphRect = attributedText.boundingRect(with: paragraphSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
首先定义一个具有一定宽度和高度的CGSize。将宽度设置为您希望段落的宽度,并将高度设置为适合内容的较大值。然后打电话
AttributeText.boundingRect(带:段落大小,选项:NSStringDrawingOptions.usesLineFragmentOrigin,上下文:nil)
其中AttributeText是段落内容。boundingRect方法返回一个CGRect,该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
}