【问题标题】:Can not line break on label标签上不能换行
【发布时间】:2016-10-27 07:28:37
【问题描述】:

我想在我的标签上设置换行符。

我试过了:

self.lblYouHaveAMatch.lineBreakMode = NSLineBreakByWordWrapping;
self.lblYouHaveAMatch.numberOfLines = 0;

我有一个这样的字符串:

"You have a match with \nfirst_name!"

我也尝试使用"\r" 而不是"\n",但它不起作用。

我在界面生成器上的标签。

但它只在我的设备上显示"You have a match with"

【问题讨论】:

  • 尝试增加你的 UILabel 高度。
  • 同意 Manishankar。你需要让你的标签框架更高(在故事板中)
  • 如果你只是想将"You have a match with"从一个名字中分离出来,你可以尝试给标签添加一个宽度约束,这样一行就只能包含“你有一个匹配”,这将强制标签在其内容长于“You have a match with”时扩展高度。
  • 将高度改为 50
  • 如果你不使用自动布局,我认为你需要在设置测试后更改标签的框架。

标签: ios objective-c label line-breaks


【解决方案1】:

您在最后一张图片上使用了自动调整蒙版 你需要在 autoresicing 中选择:右边框 太(因为现在你只有顶部和左侧边框 .你需要选择一条水平线 所以在那之后你将设置水平边框但是你 不会设置高度,所以你的高度会自动设置 因为使用指定行数 0

【讨论】:

    【解决方案2】:

    这里你只需要根据给定的文本和宽度计算标签的高度。

    NSString *sampleMultiLineText=@"1  Showing First Line\n2 Showing Second line\n3 Showing Third line";
    
    
    NSDictionary *attributes = @{NSFontAttributeName: self.lbMultiLine.font};
    
    // ios7.0
    CGRect adjustedSize = [sampleMultiLineText boundingRectWithSize:CGSizeMake(self.lbMultiLine.frame.size.width, CGFLOAT_MAX)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
    
    // 0 = Multiple lines
    self.lbMultiLine.numberOfLines = 0;
    self.lbMultiLine.lineBreakMode = NSLineBreakByWordWrapping;
    
    //setting text
    self.lbMultiLine.text = sampleMultiLineText;
    
    
    //now Update Frame as hight of label may be higher than current so all the text can be show
    [self.lbMultiLine setFrame:CGRectMake(self.lbMultiLine.frame.origin.x, self.lbMultiLine.frame.origin.y,adjustedSize.size.width,adjustedSize.size.height)];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      相关资源
      最近更新 更多