【发布时间】:2016-05-08 20:42:58
【问题描述】:
根据this answer,我可以使用intrinsicContentSize自动计算宽度/高度。这就是我所做的,但结果是0, 0
class AutoSizeUIButton: UIButton{
override func intrinsicContentSize() -> CGSize {
return CGSizeMake(self.frame.size.width, self.titleLabel!.frame.size.height)
}
}
networkButton.setTitle("something-here", forState: .Normal)
networkButton.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 20.0)!
let networkSize = networkButton.intrinsicContentSize()
print("button size", networkSize.width, networkSize.height) //prints 0.0, 0.0
我做错了吗?注意:我不想使用约束。我只想打印宽度/高度。
【问题讨论】:
-
试试这个代码 UIFont *font = [UIFont fontWithName:@"Helvetica" size:30]; NSDictionary *userAttributes = @{NSFontAttributeName:字体,NSForegroundColorAttributeName:[UIColor blackColor]}; NSString *text = @"你好"; ... const CGSize textSize = [文本 sizeWithAttributes: userAttributes];