2011-12-19 89 views

回答

1

您可以label.frame.size.height

+0

谢谢! – 2011-12-19 21:45:16

+1

@TusharChutani如果这个问题是你正在寻找的,请将它标记为已接受(左边的小标签) – MByD 2011-12-19 22:01:45

0

如果你打电话

[label sizeToFit]; 

将调整的UILabel来容纳所有内容所需的最小尺寸。然后,您可以仅使用label.frame.size.height来获取包含该文本数量的标签的高度。

+0

OP正在寻找检索高度,而不是调整内容 – barfoon 2011-12-19 21:44:11

+0

我不同意你的假设。这是一个很好的答案。 – amattn 2011-12-19 21:46:35

+0

我以为他想要的是标签中的文本所占的高度,而不是标签的高度(他最有可能在创建标签时指定的高度)。 – Casey 2011-12-19 21:48:18

1

你可能想的-[UILabel sizeThatFits:]方法获得的财产。这就是你所做的。假设您的UILabel位于变量myLabel中,并且您已将其宽度设置为任何您想要的值。

myLabel.text = @"This is my very long message which will probably need multiple lines to be displayed because it is very long."; 

CGRect bounds = myLabel.bounds; 

// Create a size that is the label's current width, and very very tall. 
CGSize prototypeSize = CGSizeMake(bounds.size.width, MAXFLOAT); 

// Ask myLabel how big it would be if it had to fit in prototypeSize. 
// It will figure out where it would put line breaks in the text to 
// fit prototypeSize.width. 
CGSize fittedSize = [myLabel sizeThatFits:prototypeSize]; 

// Now update myLabel.bounds using the fitted height and its existing width. 
myLabel.bounds = (CGRect){ bounds.origin, CGSizeMake(bounds.size.width, fittedSize.height) };