简体   繁体   中英

How add Multiple Lines in Same UILabel

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??

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

  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

在此处输入图片说明

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM