How to make multi-line UILabel text fit within pre

2019-07-02 15:43发布

问题:

I have a UILabel carefully laid out in Interface Builder with proper height and width constraints. The number of lines is set to 4. The wrapping is set to word wrap. The text is "CHECKED". The font size is very large and thus it only fits "CHECKE" and the "D" is on the second line. Writing "Checked" instead of "CHECKED" lets the font shrink (as intended) so that the whole word fits. But (the text is user given and it can be expected that the user writes fully uppercase words) having uppercase words the label does not break it/shrink the font as expected.

Do you have a suggestion as to what I might have missed? Capitalising the words (thusly only having the first letter uppercase) does work, but is not what the client wants.

Updated question

The problem seems to be unrelated to having uppercase or lowercase text. My problem could be solved by an answer to the following question:

How to make (ideally with the help of only Interface Builder) the UILabel text shrink trying to fit full words within all available lines without wrapping the text mid-word?

  • If the text "CHECKED" is too wide for a label (with more than 1 line available) it should shrink the font size instead of breaking the "D" and wrapping the single letter to the next line.
  • If the text is "one CHECKED two" and the single word "CHECKED" is already too wide for a label (with more than 1 line available) it should break between all words and shrinking the font size so that "CHECKED" still fits the middle line.

Avoiding:

one
CHECKE
D two

Thank you very much!

回答1:

When the word is bigger than the line, word wrap doesn't work. If it doesn't fit on this line, it won't fit on the next line. (same word, same size, same line size). To make it fit, the label will start putting letters on the next line.

If you allow multiple lines on your label, the OS will try to fill the lines before adjusting the font size.



回答2:

I think you're just running into a limitation on Autoshrink.

In Interface Builder:

  • add a new UILabel with Width: 230 and Height: 280
  • set the Font to System 44.0
  • set Line Break: Truncate Tail
  • set Autoshrink: Minimum Font Scale at 0.15
  • set the text of the label to test CHECKED lines

Now, drag the handle on the right edge of the label left and right... when it gets too narrow, the word CHECKED will break onto the next line.

Change CHECKED to checked and do the same thing. You should see the same behavior.

Now, try dragging the Bottom edge up and down. With either CHECKED or checked, you should see the Font Size auto shrink.

So... to do what you're trying to do, you might have to skip Autoshrink and instead do some code calculations.

Edit: further visual of what goes on...

  • Start with above values, but set the Height of the label to 170 - gives it just a little vertical padding.
  • Now, drag the left edge to make it narrower.
  • When you reach the end of the word CHECKED, and keep going, you will see the font shrink until it gets small enough that there is space for it to wrap to a 4th line.

I think you're going to need some code to get exactly what you need.