2011-12-19 94 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) };