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.
Asked
Active
Viewed 1e+01k times
4 Answers
309
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.
![](../../users/profiles/238948.webp)
devios1
- 33,997
- 43
- 149
- 241
![](../../users/profiles/6330.webp)
Kendall Helmstetter Gelner
- 73,251
- 26
- 123
- 148
-
127To clarify for noobs like myself, this would be: cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; – Brian Moeskau Jan 31 '10 at 23:06
-
46In iOS 6 and later, use `NSLineBreakByWordWrapping`, not `UILineBreakModeWordWrap`. – Aaron Brager Feb 27 '13 at 16:41
-
3You might also need the following: label.autoresizingMask = UIViewAutoresizingFlexibleHeight; – William Grand Sep 25 '13 at 19:38
-
7```label.lineBreakMode = .ByWordWrapping``` and ```label.numberOfLines = 0``` in swift – AFraser Jul 23 '15 at 21:25
-
1This 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. – David Jeske Jun 16 '16 at 23:34
-
9For 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. – jcady Jun 24 '16 at 01:18
-
@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! – Kendall Helmstetter Gelner Jun 29 '16 at 18:04
-
2label.lineBreakMode = .byWordWrapping in swift 3 – Efren Jun 13 '18 at 01:58
-
Deprecated, now use `[label setLineBreakMode:NSLineBreakByWordWrapping]` – Albert Renshaw May 21 '20 at 05:13
28
UILabel
has a property lineBreakMode
that you can set as per your requirement.
-
11"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. – Codezy Jul 14 '09 at 18:31
23
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)
![](../../users/profiles/6062.webp)
Nathan
- 10,673
- 12
- 49
- 62
-
-
I think I meant just that the naming is different. It has probably changed again in swift3 – Nathan Jan 18 '17 at 03:20
-
-
@MobileMonin my experience that is not always the case. Maybe IB changes it in some circumstances? – Nathan Feb 02 '17 at 22:54
-
3
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
![](../../users/profiles/5438240.webp)
Naishta
- 9,905
- 4
- 60
- 49
-
2Using 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? – phihag Jun 26 '19 at 12:25