UILabel sizeThatFits 不起作用 [英] UILabel sizeThatFits not working
本文介绍了UILabel sizeThatFits 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算 UITableViewCell 的高度,所以我定义了一个看起来像这样的类方法
I'm trying to calculate the height for a UITableViewCell so I've defined a class method that looks like this
+ (CGFloat)heightWithText:(NSString *)text
{
SizingLabel.text = text;
[SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)];
return (TOP_MARGIN + SizingLabel.frame.size.height + BOTTOM_MARGIN);
}
我像这样定义了 SizingLabel:
I've defined SizingLabel like this:
+ (void)initialize
{
SizingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
SizingLabel.numberOfLines = 0;
SizingLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
但是,如果我在 -heightWithText: 方法中设置断点,我会注意到 SizingLabel 的尺寸永远不会改变,因此我得到了不正确的值.这是为什么?
However, if i stick a breakpoint in the -heightWithText: method, i notice that the dimensions of SizingLabel never change and i thus get an incorrect value. Why is that?
推荐答案
+ (CGFloat)heightWithText:(NSString *)text
{
SizingLabel.text = text;
CGSize labelSize = [SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)];
return (TOP_MARGIN + labelSize.height + BOTTOM_MARGIN);
}
这篇关于UILabel sizeThatFits 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文