如何调整 UILabels 的大小以适应内容? [英] How to resize UILabels to fit content?

查看:61
本文介绍了如何调整 UILabels 的大小以适应内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序目前有 30 个 UILabel,其中可能包含一到三位数的数字.我希望我的 UILabels 随着其中的数字改变大小而水平扩展和收缩.目前文本正在缩小,UILabels 大小保持不变.

My program currently has thirty UILabels that may contain numbers between one and three digits. I want my UILabels to expand and shrink horizontally as the numbers within them change size. At the moment the text is shrinking and the UILabels size remains the same.

如果这可以仅使用 xib 文件来完成,这将比以编程方式执行此操作更可取,但如果这不可能,则两者都很棒.

If this can be done just using the xib file this would be preferable to doing so programmatically but if this isn't possible either would be great.

谢谢

推荐答案

设置标签文本后,在标签上调用sizeToFit.这将使标签刚好足以容纳文本.

After setting the text of the label, call sizeToFit on the label. This will make the label just big enough for the text.

顺便说一句 - 你为什么需要这样做?为什么不让标签足够大以容纳可能的值并让它们保持那个大小?您认为缩小标签会带来什么好处?

BTW - why do you need to do this? Why not just make the labels big enough for the possible values and leave them that size? What benefit do you think you will get by shrinking the labels?

这篇关于如何调整 UILabels 的大小以适应内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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