UILabel - Wordwrap text
Asked Answered
B

5

160

Is there any way to have a label wordwrap text as needed? I have the line breaks set to word wrap and the label is tall enough for two lines, but it appears that it will only wrap on line breaks. Do I have to add line breaks to make it wrap properly? I just want it to wrap if it can't fit it in horizontally.

Brookhouse answered 13/7, 2009 at 18:49 Comment(0)
I
320

If you set numberOfLines to 0 (and the label to word wrap), the label will automatically wrap and use as many of lines as needed.

If you're editing a UILabel in IB, you can enter multiple lines of text by pressing option+return to get a line break - return alone will finish editing.

Immigration answered 13/7, 2009 at 18:57 Comment(9)
To clarify for noobs like myself, this would be: cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;Sinusoidal
In iOS 6 and later, use NSLineBreakByWordWrapping, not UILineBreakModeWordWrap.Clarhe
You might also need the following: label.autoresizingMask = UIViewAutoresizingFlexibleHeight;Ninanincompoop
label.lineBreakMode = .ByWordWrapping and label.numberOfLines = 0 in swiftSaddletree
This does not work for me in (Xcode 7.3, iOS 9.3). My label text is wrapping in the XCode UI, but in my app the text does not wrap. I've tried setting these props via GUI Attributes Inspector, and via Code, neither works.Arlenearles
For anyone who this wasn't obvious to (like me): The UILabel must have some sort of limit on its width (either from an actual width constraint or margin constraints); otherwise it won't wrap.Ylla
@jcady, that is an excellent point now that auto-layout is more common - if the labels width is allowed to expand as much as possible, it will stay on one line. The label also must be allowed to expand in height though!Immigration
label.lineBreakMode = .byWordWrapping in swift 3Friarbird
Deprecated, now use [label setLineBreakMode:NSLineBreakByWordWrapping]Gallup
W
27

UILabel has a property lineBreakMode that you can set as per your requirement.

Wherein answered 13/7, 2009 at 19:1 Comment(1)
"I have the line breaks set to word wrap ". This only wraps on actual line breaks, it won't automatically break once it is to long.Brookhouse
A
27

In Swift you would do it like this:

    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.numberOfLines = 0

(Note that the way the lineBreakMode constant works is different to in ObjC)

Aesthetics answered 29/7, 2016 at 7:56 Comment(6)
Care to elaborate on how it's different?Wittol
I think I meant just that the naming is different. It has probably changed again in swift3Aesthetics
Line break mode should be set to word wrapping be defaultArmistead
@MobileMonin my experience that is not always the case. Maybe IB changes it in some circumstances?Aesthetics
label.lineBreakMode = NSLineBreakByWordWrapping in Obj CHarlanharland
With Swift 4 I had to use NSLineBreakMode.byWordWrappingNonfeasance
B
4

Xcode 10, Swift 4

Wrapping the Text for a label can also be done on Storyboard by selecting the Label, and using Attributes Inspector.

Lines = 0 Linebreak = Word Wrap

enter image description here

Beamy answered 17/1, 2019 at 16:49 Comment(1)
Using a branBrand-New label, directly adding it to the view, this still generates a single-line label (with a width larger than the phone's width) for me, despite setting lines to 0 and linebreak to Word Wrap. Is there another setting that could be needed? Do I need to attach additional code?Deutzia
D
0

Xcode 12.5.1, Swift 5.

I found that even though I had Lines = 0 and LineBreak = Word Wrap set, it still didn't wrap the long label text. Double check your label constraints in IB (or wherever you set them).

I found that sometimes IB tries to fix the constraint settings and adds a >= or <= constraint for leading and trailing edges.

This label constraint will not wrap the text:

enter image description here


But this will:

enter image description here

Notice the >= on the Label.trailing edge in the first picture. Set this to = and the text should wrap.

Darsey answered 9/1, 2022 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.