【问题标题】:UILabel Multiple Line in iOS6iOS6中的UILabel多行
【发布时间】:2013-08-13 10:52:05
【问题描述】:

我必须在 UILabel 中显示多行(如果文本很大)。下面是我的代码。我为不同的 iOS 版本使用单独的属性。请帮帮我..

    labelLocation.numberOfLines=2;
    labelLocation.font=[UIFont systemFontOfSize:25];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
        labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
        labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
    }else{
        labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
        labelLocation.minimumFontSize=10;
    }
    labelLocation.text=@"Can we make UILabeltext in 2 lines if name is large";

【问题讨论】:

  • 您能详细说明一下您遇到的问题吗?顺便说一句,两行代码真的适合标签的框架吗?
  • 我有尺寸为 230x40 的标签。当文本很大时,我需要将文本分两行显示。
  • 检查我编辑的代码并尝试增加标签的框架。
  • 你可以试试 labelLocation.numberOfLines=0;
  • 你的问题解决了吗?

标签: iphone objective-c xcode ios6 uilabel


【解决方案1】:

这两行一起工作

labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;

【讨论】:

  • 我增加了高度,现在它可以工作了。但是 minimumScaleFactor 不起作用。任何评论!
  • minimumScaleFactor 表示如果不填充标签中的整个文本,您的标签将缩小到您定义的比例因子。它不会减少更多的字体大小。阅读它stackoverflow.com/a/15332231/1305001
【解决方案2】:

你可以设置

 yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
 yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)

并检查您的插座是否设置正确。

【讨论】:

  • 感谢支持,但我已经在使用该属性了。看第一行代码。
  • 最多两行。而且我使用了 NSLineBreakByWordWrapping 属性。不工作
  • 根据您的代码,如果它进入 else 条件,那么您需要更改 UILineBreakModeTailTruncation。 UILineBreakModeTailTruncation 仅适用于一个行标签。
  • 请检查标签的高度和宽度。
【解决方案3】:

试试这个labelLocation.numberOfLines=0;

【讨论】:

    【解决方案4】:

    我想,您的标签必须要小高度。 systemFontOfSize 25 中的两行需要高度约为 60。 标签过小,系统不换行。

    【讨论】:

    • 我也在使用 minimumScaleFactor 属性!
    • 只有numberOfLines = 1时有效;
    【解决方案5】:

    把你的代码改成这个

    labelLocation.numberOfLines=0;
    labelLocation.font=[UIFont systemFontOfSize:40];
    labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
    labelLocation.text=@"Can we make UILabeltext in 2 lines if name is large";
    

    【讨论】:

      【解决方案6】:

      我个人建议您计算显示文本所需的高度,然后将其显示到标签上...切勿对 UITextView 和 UILable 等文本显示组件进行硬编码。

       NSString *str = @"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";
      
      
      `UIFont * myFont = [UIFont fontWithName:@"Arial" size:12];//specify your font details here
      //then calculate the required height for the above text.
      
      CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
      

      //根据你从上面得到的高度初始化你的标签..你可以放任何你喜欢的宽度......

       UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];
      
      myLable.text = str;
      myLable.numberOfLines=0;
      myLable.font=[UIFont fontWithName:@"Arial" size:12];
      //myLable.backgroundColor=[UIColor redColor];
      myLable.lineBreakMode = NSLineBreakByWordWrapping;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-02
        • 1970-01-01
        • 2023-03-17
        • 2013-04-07
        相关资源
        最近更新 更多