【发布时间】:2014-08-02 11:13:34
【问题描述】:
我正在努力根据它可以接受的可变大小文本来获得 UILabel 所需的高度。经过数小时的研究,我还没有找到实现这一目标的可行方法。目前我的代码如下:
func getHeightForTitle(postTitle: NSString) -> CGFloat {
// Get the height of the font
let constraintSize = CGSizeMake(self.cellTextWidth, CGFloat.max)
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
let labelSize = postTitle.boundingRectWithSize(constraintSize,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes,
context: nil)
return labelSize.height
}
然而,这会引发以下错误:
2014-08-02 12:09:37.370 Testing App[8365:351906] - [_TtCSs23_ContiguousArrayStorage00007FD26C15F708 pointSize]: unrecognized selector sent to instance 0x11e640050
这总是在 let labelSize = postTitle... 方法中抛出,我相信它归结为属性变量。然而,我可能错了。任何帮助表示赞赏,非常感谢!
请注意:这是针对 iOS 8、Swift 开发项目的。
【问题讨论】:
标签: nsstring swift uilabel ios8