Ios Swift:boundingRectWithSize返回多行标签的错误高度

Ios Swift:boundingRectWithSize返回多行标签的错误高度,ios,swift,string,uilabel,Ios,Swift,String,Uilabel,我在String类上使用扩展来获得在多行标签中显示字符串所需的高度 extension String { func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat { let constraintRect = CGSize(width: width, height: CGFloat.max) let option = NSStringDrawingOptions

我在String类上使用扩展来获得在多行标签中显示字符串所需的高度

extension String {
    func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: CGFloat.max)

        let option = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)

        let boundingBox = self.boundingRectWithSize(constraintRect, options: option, attributes: [NSFontAttributeName: font], context: nil)
        print("ex width \(boundingBox.width)")
        print("ex height \(boundingBox.height)")
        return boundingBox.height
    }
}
我正在为rowatinexpath调用它的高度

let myRandomString = "My baby looks like she's ashamed and wants to apologise for something bad she did."
let labelWidth = UIScreen.mainScreen().bounds.width-16
let labelHeight = myRandomString.heightWithConstrainedWidth(labelWidth,font: UIFont.boldSystemFontOfSize(17))
我得到:

ex width 359.0
ex height 40.57421875
但当我使用xcode的view Debugging选项时,我得到了以下结果:

我的UILabel就是这样设置的:

正如你所看到的,我的宽度是正确的(我强迫它),但我的高度是不正确的,我不知道为什么

感谢和问候,
Eric

仅适用于多行使用
。UsesLineFragmentOrigin

仅适用于多行使用
。UsesLineFragmentOrigin