UILabel根据数组大小调整大小

时间:2013-12-09 21:51:23

标签: objective-c nsstring uilabel

我已经尝试了一切,但似乎无法使其发挥作用。我有一个名为“HomePlayersString”的数组(不是理想的称它为字符串,因为它是我知道的数组)这个数组包含许多短字符串。字符串用/ n分隔,因此它们应该打印在标签的每一行上。事情是我似乎无法使Label重新调整为NSArray的大小。

以下是我从void viewdidload

中提出的代码
HomePlayersLabel.text = [HomePlayersString componentsJoinedByString:@"\n"];
[HomePlayersLabel setNumberOfLines:0];
[HomePlayersLabel sizeToFit];
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [HomePlayersString sizeWithFont:HomePlayersLabel.font constrainedToSize:maximumLabelSize lineBreakMode:HomePlayersLabel.lineBreakMode];

CGRect newFrame = HomePlayersLabel.frame;
newFrame.size.height = expectedLabelSize.height;
HomePlayersLabel.frame = newFrame;

我得到的错误如下:

no visible @interface for 'NSArray' declared the selector 'sizewithfont:contrainedToSize:lineBreakMode:'

1 个答案:

答案 0 :(得分:2)

sizeWithFont:variants是NSString UIKit Additions的成员:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

因此,您需要将其发送到HomePlayerLabel.text,如

CGSize expectedLabelSize = [HomePlayersLabel.text sizeWithFont:HomePlayersLabel.font constrainedToSize:maximumLabelSize lineBreakMode:HomePlayersLabel.lineBreakMode];