根据文本调整 UILabel 高度 [英] Adjust UILabel height depending on the text

查看:32
本文介绍了根据文本调整 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's 的高度,以便文本可以适应.我正在使用 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;

这篇关于根据文本调整 UILabel 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆