根据文本调整UILabel高度
考虑我在 UILabel
(一长串动态文本)中有以下文本:
Consider I have the following text in a UILabel
(a long line of dynamic text):
由于外星人的军队远远超过了团队,玩家必须使用后启示世界的优势,例如寻求掩护在垃圾箱,柱子,汽车,瓦砾和其他物品后面。
Since the alien army vastly outnumbers the team, players must use the post-apocalyptic world to their advantage, such as seeking cover behind dumpsters, pillars, cars, rubble, and other objects.
我想调整 UILabel的
高度, 。我使用 UILabel
的以下属性使文本内部换行。
I want to resize the UILabel's
height so that the text can fit in. I'm using following properties of UILabel
to make the text within to wrap.
myUILabel.lineBreakMode = UILineBreakModeWordWrap;
myUILabel.numberOfLines = 0;
请让我知道如果我不朝正确的方向。感谢。
Please let me know if I'm not heading in the right direction. Thanks.
sizeWithFont constrainedToSize:lineBreakMode:
。如何使用它的示例如下:
sizeWithFont constrainedToSize:lineBreakMode:
is the method to use. An example of how to use it is below:
//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;