How to make multi-line UILabel text fit within predefined width without wrapping mid-word

≯℡__Kan透↙ 提交于 2019-12-23 17:21:26

问题


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.



来源:https://stackoverflow.com/questions/42890965/how-to-make-multi-line-uilabel-text-fit-within-predefined-width-without-wrapping

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