如何在同一个UILabel中添加多行

时间:2013-10-19 04:01:38

标签: ios uilabel

UILabel中有多行文字吗?

我不希望视图中有多个标签。

如何在单个UILabel中添加多行?

3 个答案:

答案 0 :(得分:1)

是的,有办法。只需要添加UILabel的两个属性,即

  1. NumberOfLines=0它允许您在UILabel

  2. 中添加多行
  3. LineBreakMode = NSLineBreakByWordWrapping它可以让你逐句打破你的句子。您也可以根据您的要求进行更改。

    [YourLabel setNumberOfLines:0];
    [YourLabel setLineBreakMode:NSLineBreakByWordWrapping];
    
  4. 您也可以在界面构建器

    中设置这两个属性

    enter image description here

    enter image description here

答案 1 :(得分:0)

这是一个示例代码

    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;

动态计算UILabel的高度 请参考以下帖子 Adjust UILabel height depending on the text

答案 2 :(得分:0)

以下是示例代码:

    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];

确保您的标签高度应该更高,以免线条总数变得可见。