153

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.

TheNeil
  • 1,985
  • 2
  • 17
  • 36
Codezy
  • 5,340
  • 7
  • 35
  • 47

4 Answers4

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.

devios1
  • 33,997
  • 43
  • 149
  • 241
Kendall Helmstetter Gelner
  • 73,251
  • 26
  • 123
  • 148
  • 127
    To clarify for noobs like myself, this would be: cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; – Brian Moeskau Jan 31 '10 at 23:06
  • 46
    In iOS 6 and later, use `NSLineBreakByWordWrapping`, not `UILineBreakModeWordWrap`. – Aaron Brager Feb 27 '13 at 16:41
  • 3
    You 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
  • 1
    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. – David Jeske Jun 16 '16 at 23:34
  • 9
    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. – 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
  • 2
    label.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.

ozgur
  • 41,172
  • 16
  • 73
  • 106
Greg
  • 1,116
  • 1
  • 10
  • 26
  • 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)

Nathan
  • 10,673
  • 12
  • 49
  • 62
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

enter image description here

Naishta
  • 9,905
  • 4
  • 60
  • 49
  • 2
    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? – phihag Jun 26 '19 at 12:25