9

Under certain circumstances, UILabel seems to bring an extra word to new line even when there is enough space for it, for example,

enter image description here

If one more word is appended,

enter image description here

Even if I force the width of the label to become something like below, it still moves the word consists of "c"s to the next line,

enter image description here

I've tried twisting the configuration of the UILabel, but seems it behaves the same unless I set the line breaking mode to character wrap, below is the configuration for the above cases,

enter image description here

And the constraints (in the first two cases, the trailing ),

enter image description here

Is there any reason for this particular behaviour and can I fix this? It looks weird in this way leaving that space emptied.

peakingcube
  • 1,388
  • 15
  • 32

6 Answers6

3

this is the default behavior since iOS 11, to fix orphaned words. No way to shut it off

to fix it use the text as attributed text

or use UItextview and turn of the scroll, edit option

or use the custom label here Get each line of text in a UILabel

Abuzeid
  • 962
  • 10
  • 14
0

You should set the line break to character wrap, because the letters after the space will be recognized as a word.

Jack Liu
  • 17
  • 1
0

Hey I know this is late but I just figured out that you can cheat the system by adding a bunch of spaces at the end of the text.

Ethan Zhao
  • 229
  • 1
  • 6
  • 18
0

If text of UILable may be changed, then it's still possible to use quick-dirty hack with "\n" - new line symbol.

Text "Aaaaaaaaaaaaaa bbb cccccccccc\ndddddd" will force UILabel to display:

Aaaaaaaaaaaaaa bbb cccccccccc
ddddddd

In Interface Builder new line can be inputted with Ctrl + Enter

Artem
  • 331
  • 1
  • 13
-1

If u use wordWrap, it tries to keep words in full form, as a result of this, it goes to next line. If you use character wrap, it will break on characters, and push the chars onto next line.

-2

For Example:- My name is ABCXXXX and I (have space here) love myself.

Solution:- Use NSMutableAttributedText and write love\n. It will make the "love" be in the space myself in the next line.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '22 at 20:03