iOS UILabel bounding rectangle not sized correctly

不问归期 提交于 2020-06-29 13:06:09

问题


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

  • Label has variable text length
  • Font set to 40
  • Minimum font-size set to 20
  • Number of lines set to 3

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).

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?


回答1:


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.

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.

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



来源:https://stackoverflow.com/questions/44410493/ios-uilabel-bounding-rectangle-not-sized-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!