func ZuoHeightWithConstrainedWidth ( width : CGFloat , font : UIFont, str : NSString ) -> CGFloat {
let constraintRect = CGSize ( width : width , height : CGFloat . max )
let boundingBox = str.boundingRectWithSize ( constraintRect , options : NSStringDrawingOptions . UsesLineFragmentOrigin , attributes : [ NSFontAttributeName : font ], context : nil )
// 返回boundingBox的。高度
return boundingBox.height
}
这段代码展示了如何使用Swift创建一个计算字符串在指定宽度下高度的方法。通过设定约束宽度和字体,利用`boundingRectWithSize`计算字符串的边界框高度,从而实现高度自适应。

7215

被折叠的 条评论
为什么被折叠?



