Multiple lines of text in UILabel

23 Solutions Collect From Internet About “Multiple lines of text in UILabel”

I found a solution.

One just has to add the following code:

// Swift
textLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
textLabel.numberOfLines = 0 

// For Swift >= 3
performanceLabel.lineBreakMode = .byWordWrapping // notice the 'b' instead of 'B'
performanceLabel.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.

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

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;

Use this to have multiple lines of text in UILabel:

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


textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0
myUILabel.numberOfLines = 0;
myUILabel.text = @"your long string here";
[myUILabel sizeToFit];

If you have to use the:

myLabel.numberOfLines = 0;

property you can also use a standard line break ("\n"), in code, to force a new line.

You can use \r to go to next line while filling up the UILabel using NSString.

UILabel * label;

label.text = [NSString stringWithFormat:@"%@ \r %@",@"first line",@"seconcd line"];

lets try this

textLabel.lineBreakMode = UILineBreakModeWordWrap;          

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

The solution above does’t work in my case. I’m doing like this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    // ...

    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
    return size.height + 20;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        // ...
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Georgia-Bold" size:18.0];

    // ...

    UILabel *textLabel = [cell textLabel];
    CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0]
                                        constrainedToSize:CGSizeMake(240.0, 480.0)

    cell.textLabel.frame = CGRectMake(0, 0, size.width + 20, size.height + 20);


Use story borad : select the label to set number of lines to zero……Or Refer this

enter image description here

UILabel *helpLabel = [[UILabel alloc] init];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:label];
helpLabel.attributedText = attrString;
// helpLabel.text = label;

helpLabel.textAlignment = NSTextAlignmentCenter;
helpLabel.lineBreakMode = NSLineBreakByWordWrapping;
helpLabel.numberOfLines = 0;

For some reasons its not working for me in iOS 6 not sure why. Tried it with and without attributed text. Any suggestions.

Try using this code:

lblName.numberOfLines = 0;
[lblName sizeToFit];

you should try this:

-(CGFloat)dynamicLblHeight:(UILabel *)lbl
    CGFloat lblWidth = lbl.frame.size.width;
    CGRect lblTextSize = [lbl.text boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
    return lblTextSize.size.height;
UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[labelName sizeToFit];
labelName.numberOfLines = 0;
labelName.text = @"Your String...";
[self.view addSubview:labelName];

You can do that via the Storyboard too:

  1. Select the Label on the view controller
  2. In the Attribute Inspector, increase the value of the Line option (Press Alt+Cmd+4 to show Attributes Inspector)
  3. Double click the Label in the view controller and write or paste your text
  4. Resize the Label and/or increase the font size so that the whole text could be shown
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
[textLabel sizeToFit];
textLabel.numberOfLines = 0;
textLabel.text = @"Your String...";

Already answered, but you can do it manually in the storyboard too. Under Attributes Inspector for the Label, you can change Line Breaks to Word Wrap (or character wrap).

I agree with Jarred Olson, NSLineBreakByWordWrapping is default.
I added property sizeToFit like the guide of Gurumoorthy Arumugam to fix it.

If you want the font inside your label to adjust itself to fit into the boundaries of the label. You can use :

textLabel.adjustsFontSizeToFitWidth = YES;

Thanks all.

These things helped me

Change these properties of UILabel

label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByWordWrapping;

And while giving input String use \n to display different words in different lines.

Example :

 NSString *message = @"This \n is \n a demo \n message for \n stackoverflow" ;

In this function pass string that you want to assign in label and pass font size in place of self.activityFont and pass label width in place of 235, now you get label height according to your string.
it will work fine.

-(float)calculateLabelStringHeight:(NSString *)answer
    CGRect textRect = [answer boundingRectWithSize: CGSizeMake(235, 10000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.activityFont} context:nil];
    return textRect.size.height;


Set below either in code or in storyboard itself

Label.lineBreakMode = NSLineBreakByWordWrapping;
Label.numberOfLines = 0;

and please don’t forget to set left, right, top and bottom constraints for label otherwise it won’t work.

Swift 3
Here is solution with swift 3. Just copy & execute this code to see result

let label = UILabel()
label.numberOfLines = 0
let stringValue = "A label\nwith\nmultiline text."
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

This code is returning size height according to text

+ (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font
    CGFloat result = font.pointSize+4;
    if (text) 
        CGSize size;

        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, 999)
        size = CGSizeMake(frame.size.width, frame.size.height+1);
        result = MAX(size.height, result); //At least one row
    return result;