UILabel word wrapping brings extra word to next line when there is enough space for it
Asked Answered
S

7

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.

Sciomancy answered 23/2, 2018 at 2:4 Comment(5)
#48028547Genie
@Genie I see, thanks for you help!Sciomancy
I did find a way but I am still unsure on its reliability(resizing text) at the moment but the layout is def old UILabel. I will update those answers when I feel good about the answer.Genie
Found any solution to it?Deposal
it seems next line should at least have two words.Osteopath
V
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

Variform answered 28/5, 2020 at 13:47 Comment(1)
Apart from this we can do this ans also https://mcmap.net/q/1227751/-uilabel-word-wrapping-brings-extra-word-to-next-line-when-there-is-enough-space-for-it It really helpsRunofthemill
A
2

set the 'strategy' to 'Hangul word priority'. it would help

Abortionist answered 23/10, 2023 at 10:6 Comment(0)
H
0

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

Hundredfold answered 23/2, 2018 at 2:51 Comment(0)
P
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.

Paramnesia answered 18/7, 2019 at 2:35 Comment(0)
P
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

Point answered 21/11, 2019 at 12:42 Comment(0)
S
-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.

Sloan answered 18/7, 2019 at 2:38 Comment(0)
S
-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.

Skirr answered 17/7, 2022 at 9:3 Comment(1)
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.Eschatology

© 2022 - 2024 — McMap. All rights reserved.