【发布时间】:2019-12-31 01:54:53
【问题描述】:
我正在使用这种方法在 PDF 中绘制文本:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineBreakMode = .byWordWrapping
let textAttributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont(name: "MySpecialFont", size: 16)!,
NSAttributedString.Key.foregroundColor: UIColor.black
]
let attributedText = NSAttributedString(
string: myTextArray.first,
attributes: textAttributes
)
let textRect = CGRect(
x: 400,
y: 40,
width: 80,
height: 40
)
attributedText.draw(in: textRect)
上面的文字很好。但是,有时,传递给它的字符串似乎太长,并且持续 3 行而不是 2 行。在这些情况下,我希望 textRect 更高。基本上要知道需要多少行,以便可以调整textRect。
NSAttributedString 中有几个函数可以提供字符串长度,但如果它在一行中,那就是长度。
有没有办法知道最终的attributedText 将在textRect 中占用多少行?
【问题讨论】:
标签: ios swift string xcode nsattributedstring