【发布时间】:2020-03-03 16:18:29
【问题描述】:
我找到了几个关于如何使用给定文本和 UIFont 计算 UILabel 高度的好答案:
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font: font], context: nil)
return boundingBox.height
}
}
但是,我想更进一步,用给定的文本、字体和行数计算高度。这甚至可能吗?所以,如果高度应该是 2 行,我想要 2 行的高度。
谢谢。
【问题讨论】:
-
这是您要找的东西吗? stackoverflow.com/questions/7174007/…
标签: swift