iOS UILabel边界矩形大小不正确 [英] iOS UILabel bounding rectangle not sized correctly

查看:128
本文介绍了iOS UILabel边界矩形大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel(以黄色突出显示),它具有以下条件.

I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.

  • 标签的文字长度可变
  • 字体设置为40
  • 最小字体大小设置为20
  • 行数设置为3

尽管这看起来像是重复的问题,但我相信并非如此.我遇到的问题是,当文本缩小到20点后超出了可用的3行长度时,UILabel的边界框的大小设置不正确(即注意文本上方和下方的多余间距).

Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).

最终结果应该是没有任何间距的UILabel.在将行数设置为3的同时,是否有解决方案?

The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?

推荐答案

那看起来像个bug.如果增加基本字体大小,则会看到空格增加.另外,如果您在运行时检查布局,则会看到要计算的内容大小太大.

That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.

我的猜测是,UILabel使用原始字体大小(40)来计算3行文本的内容大小,而没有考虑到截断之前已经减小了字体大小.

My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.

我摆弄内容拥抱/压缩优先级,但也无法使它起作用.

I fiddled with content hugging/compression priorities but could not make it work either.

我发现的唯一解决方法是将字体大小手动设置为20.这将为您提供所需的框架.

The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.

这篇关于iOS UILabel边界矩形大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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