0

Any way to have multiple lines of text in UILabel ?

I dont wish to more than 1 label in the view.

How to add multiple lines in a single UILabel??

rmaddy
  • 298,130
  • 40
  • 468
  • 517
PSA
  • 3
  • 1

3 Answers3

1

Yes there is a way. Just you need to add two property of UILabel i.e.

  1. NumberOfLines=0 It'll allow you to add multiple lines in a UILabel

  2. LineBreakMode = NSLineBreakByWordWrapping It'll allow you to break your sentence by word. You can also change it according to your requirement.

    [YourLabel setNumberOfLines:0];
    [YourLabel setLineBreakMode:NSLineBreakByWordWrapping];
    

You can also set this two property form your interface builder

enter image description here

enter image description here

Tapas Pal
  • 6,566
  • 6
  • 32
  • 76
0

here is a sample code

    UILabel  *pHolder1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 245, 45)];
    pHolder1.backgroundColor = [UIColor clearColor];
    pHolder1.font = [UIFont systemFontOfSize:14.0f];
    pHolder1.numberOfLines =0;
    pHolder1.lineBreakMode = NSLineBreakByWordWrapping;
    pHolder1.textAlignment = NSTextAlignmentCenter;

Dynamically calculate the height of UILabel please refer the below post Adjust UILabel height depending on the text

Community
  • 1
  • 1
Rocker
  • 1,217
  • 7
  • 15
0

Here is sample code:

    UILabel *lblUsername=[[UILabel alloc] init];
    StoryTextSize = [storytext sizeWithFont:[UIFont fontWithName:@"Georgia" size:13.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    lblUsername.frame=CGRectMake(20, 5, [[UIScreen mainScreen] bounds].size.width-40, StoryTextSize.height);
    lblUsername.textColor=[UIColor blackColor];
    lblUsername.text=[NSString stringWithFormat:@"%@",[[tblRecords objectAtIndex:indexPath.row] valueForKey:@"username"]];
    lblStoryText.numberOfLines=nooflines;
    lblStoryText.backgroundColor=[UIColor clearColor];
    [self.view addSubview:lblStoryText];

make sure that your label height should be more so total no of lines become visible.

Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38