Multiple lines of text in UILabel

388,907

Solution 1

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.

Solution 2

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).

Solution 3

The best solution I have found (to an otherwise frustrating problem that should have been solved in the framework) is similar to vaychick's.

Just set number of lines to 0 in either IB or code

myLabel.numberOfLines = 0;

This will display the lines needed but will reposition the label so its centered horizontally (so that a 1 line and 3 line label are aligned in their horizontal position). To fix that add:

CGRect currentFrame = myLabel.frame;
CGSize max = CGSizeMake(myLabel.frame.size.width, 500);
CGSize expected = [myString sizeWithFont:myLabel.font constrainedToSize:max lineBreakMode:myLabel.lineBreakMode]; 
currentFrame.size.height = expected.height;
myLabel.frame = currentFrame;

Solution 4

Use this to have multiple lines of text in UILabel:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

Swift:

textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0

Solution 5

myUILabel.numberOfLines = 0;
myUILabel.text = @"your long string here";
[myUILabel sizeToFit];
Share:
388,907
Ilya Suzdalnitski
Author by

Ilya Suzdalnitski

Full-stack engineer with a focus on delivering reliable and maintainable software. I have a strong preference for functional programming and believe that it is the most important aspect of producing quality software. Passionate about continuous improvement, and always eager to learn and apply new technology.

Updated on July 21, 2022

Comments

  • Ilya Suzdalnitski
    Ilya Suzdalnitski almost 2 years

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