I'm using boundingRect
to calculate the height of a String, I'm using Ubuntu as my font, and when I supply .usesFontLeading
as one of the options alongside .usesLineFragmentOrigin
the height is completely different to what I expect.
Here is the code I'm using for reference:
func height(withConstrainedWidth width: CGFloat, font: UIFont, maxLines: CGFloat = 0) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: [
NSAttributedString.Key.font: font
], context: nil)
var size = boundingBox.size
if maxLines > 0 {
size.height = min(size.height, CGFloat(maxLines) * font.lineHeight)
}
return ceil(size.height)
}
Firstly, what does .usesFontLeading
even do, and why is it that the result I'm getting an unexpected result.
Note: self is String
because I've extended the String
class.