Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

UILabel with text

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
438 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...