• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

调整 UILabels 的大小以适应内容?

用户头像
it1352
帮助1

问题说明

我的程序目前有 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.

谢谢

正确答案

#1

设置标签文本后,在标签上调用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?

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgkjgi
系列文章
更多 icon
同类精品
更多 icon
继续加载