Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple lines of text in UILabel

Is there a way to have multiple lines of text in UILabel like in the UITextView or should I use the second one instead?

like image 660
Ilya Suzdalnitski Avatar asked Jun 13 '09 07:06

Ilya Suzdalnitski


People also ask

What is a multiline label?

Property Value True if the label control is multiline; otherwise, False. A multi-line label control renders the label's text on more than one line. If the WordWrap property is set to True, long lines will be wrapped to the following line.

How do you wrap text in UILabel?

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.


2 Answers

Set the line break mode to word-wrapping and the number of lines to 0:

// Swift textLabel.lineBreakMode = .byWordWrapping textLabel.numberOfLines = 0  // Objective-C textLabel.lineBreakMode = NSLineBreakByWordWrapping; textLabel.numberOfLines = 0;  // C# (Xamarin.iOS) textLabel.LineBreakMode = UILineBreakMode.WordWrap; textLabel.Lines = 0;   

Restored old answer (for reference and devs willing to support iOS below 6.0):

textLabel.lineBreakMode = UILineBreakModeWordWrap; textLabel.numberOfLines = 0; 

On the side: both enum values yield to 0 anyway.

like image 66
Ilya Suzdalnitski Avatar answered Sep 19 '22 14:09

Ilya Suzdalnitski


In IB, set number of lines to 0 (allows unlimited lines)

When typing within the text field using IB, use "alt-return" to insert a return and go to the next line (or you can copy in text already separated out by lines).

like image 41
Kendall Helmstetter Gelner Avatar answered Sep 18 '22 14:09

Kendall Helmstetter Gelner