UILabel - 自动调整标签大小以适合文本?
是否可以自动调整 UILabel 框/边界的大小以适应所包含的文本? (我不在乎它最终是否比显示屏大)
因此,如果用户输入“你好”或“我的名字很长,我希望它适合这个盒子”,它永远不会被截断,并且标签会被“加宽” ' 因此?
Is it possible to auto-resize the UILabel box/bounds to fit the contained text?
(I don't care if it ends up larger than the display)
So if a user enters "hello" or "my name is really long i want it to fit in this box", it is never truncated and the label is 'widened' accordingly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
请查看我的要点,其中我为
UILabel
创建了一个非常相似的类别,我的类别让UILabel
拉伸其高度以显示所有内容:https://gist.github.com/1005520或者查看这篇文章:https://stackoverflow.com/a/7242981/662605
这会拉伸高度,但您可以轻松更改它以进行工作另一种方式并用这样的方法拉伸宽度,我相信这就是您想要做的:
您可以更简单地使用从
UIView
类,但为了安全起见,将行数设置为 1。iOS 6 更新
如果您使用 AutoLayout,那么您就有一个内置的解决方案。通过将行数设置为 0,框架将适当调整标签大小(添加更多高度)以适合您的文本。
iOS 8 更新
sizeWithFont:
已弃用,因此请使用sizeWithAttributes:
代替:Please check out my gist where I have made a category for
UILabel
for something very similar, my category lets aUILabel
stretch it's height to show all the content: https://gist.github.com/1005520Or check out this post: https://stackoverflow.com/a/7242981/662605
This would stretch the height, but you can change it around easily to work the other way and stretch the width with something like this, which is I believe what you want to do:
You could more simply use the
sizeToFit
method available from theUIView
class, but set the number of lines to 1 to be safe.iOS 6 update
If you are using AutoLayout, then you have a built in solution. By setting the number of lines to 0, the framework will resize your label appropriately (adding more height) to fit your text.
iOS 8 update
sizeWithFont:
is deprecated so usesizeWithAttributes:
instead:使用
[label sizeToFit];
将从 Daniels 类别中获得相同的结果。尽管我建议使用自动布局并让标签根据约束自行调整大小。
Using
[label sizeToFit];
will achieve the same result from Daniels Category.Although I recommend to use autolayout and let the label resize itself based on constraints.
如果我们希望 UILabel 根据文本大小缩小和扩展,那么带有自动布局的故事板是最好的选择。以下是实现此目的的步骤 步骤
UILabel
的numberOfLines
属性设置0
。给予顶部、前导和尾随空格引脚约束。
更新框架
,然后单击修复错位
。现在,如果文本较少,此 UILabel 将缩小;如果文本较多,则此 UILabel 将扩展。If we want that
UILabel
should shrink and expand based on text size then storyboard with autolayout is best option. Below are the steps to achieve thisSteps
Put UILabel in view controller and place it wherever you want. Also put
0
fornumberOfLines
property ofUILabel
.Give it Top, Leading and Trailing space pin constraint.
Update Frame
and click onFix Misplacement
. Now this UILabel will shrink if text is less and expand if text is more.这并不像其他一些答案那么复杂。
固定左侧和顶部边缘
只需使用自动布局添加约束即可固定标签的左侧和顶部边缘。
之后它会自动调整大小。
注意
使用自动布局时无需设置
sizeToFit
。我的示例项目的完整代码在这里:myLabel.preferredMaxLayoutWidth = 150 // 或其他内容
。 (我还将按钮固定在标签底部,以便当标签高度增加时它会向下移动。)UITableViewCell
内动态调整大小的标签然后看这个答案。This is not as complicated as some of the other answers make it.
Pin the left and top edges
Just use auto layout to add constraints to pin the left and top sides of the label.
After that it will automatically resize.
Notes
No need to set
sizeToFit
when using auto layout. My complete code for the example project is here:myLabel.preferredMaxLayoutWidth = 150 // or whatever
in code. (I also pinned my button to the bottom of the label so that it would move down when the label height increased.)UITableViewCell
then see this answer.使用
[label sizeToFit];
调整UILabel中的文本Use
[label sizeToFit];
to adjust the text in UILabel这是我发现适合我的情况的方法:
1)使用自动布局时,UILabel 的高度有 >= 0 约束。宽度是固定的。
2)将文本分配到 UILabel 中,此时它已经有一个超级视图(不确定这有多重要)。
3) 然后,执行以下操作:
现在标签的高度已设置适当。
Here's what I am finding works for my situation:
1) The height of the UILabel has a >= 0 constraint using autolayout. The width is fixed.
2) Assign the text into the UILabel, which already has a superview at that point (not sure how vital that is).
3) Then, do:
The height of the label is now set appropriately.
我根据上面丹尼尔的回复创建了一些方法。
I created some methods based Daniel's reply above.
这就是分类法。您必须先设置文本,然后调用此方法来调整 UILabel 的高度。
This is category method. You must set text first, than call this method to adjust UILabel's height.
您可以使用两种方式根据文本和其他相关控件调整标签大小
适用于 iOS 7.0 及更高版本
iOS 7.0 之前的更高版本,这可用于计算标签大小
// 基于 labelTextHeight 重新构建其他控件
如果您不想计算标签文本的大小可以在 UILabel 实例上使用 -sizeToFit 作为-
// 之后您可以获得标签框架来重新构建其他相关控件
You can size your label according to text and other related controls using two ways-
For iOS 7.0 and above
before iOS 7.0 this could be used to calculate label size
// reframe other controls based on labelTextHeight
If you do not want to calculate the size of the label's text than you can use -sizeToFit on the instance of UILabel as-
// after this you can get the label frame to reframe other related controls
sizeToFit() 方法在约束条件下不能很好地发挥作用,但从 iOS 9 开始,这就是您所需要的 -
the sizeToFit() method doesn't play well with constraints, but as of iOS 9 this is all you need -
[label sizeToFit];
[label sizeToFit];
in viewDidLoad我在自动布局方面遇到了很大的问题。
表格单元格内有两个容器。第二个容器根据项目描述(0 - 1000 个字符)调整大小,并且应根据它们调整行大小。
缺少的成分是描述的底部约束。
我已将动态元素的底部约束从 = 0 更改为 >= 0。
I had a huge problems with auto layout.
We have two containers inside table cell. Second container is resized depending on Item description (0 - 1000 chars), and row should be resized based on them.
The missing ingredient was bottom constraint for description.
I've changed bottom constraint of dynamic element from = 0 to >= 0.
每次都适合! :)
Fits everytime! :)
您可以显示一行输出,然后设置属性 Line=0 并显示多行输出,然后设置属性 Line=1 及更多
you can show one line output then set property Line=0 and show multiple line output then set property Line=1 and more
还有这样的做法:
There's also this approach: