【问题标题】:Multiline UILabel?多行UILabel?
【发布时间】:2011-05-20 10:21:30
【问题描述】:

我需要插入UILabel 多行文本。我执行以下操作:

NSMutableString * spName = [[NSMutableString alloc ]initWithString:@""];

for (NSUInteger i=0; i<arrEx.count; ++i)
{
    ExInfo * exInf = [arrEx objectAtIndex:i];
    [spName appendString:[MyObject getName:exInf.spNum]];
    [spName appendString:@" "];
    [spName appendString:exInf.totalTime];
    [spName appendString:@"\n"];        
}

CGSize size = [spName sizeWithFont:[UIFont systemFontOfSize:14] 
                 constrainedToSize:constraint
                     lineBreakMode:UILineBreakModeWordWrap];

[cell.exsInfoLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, top, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), size.height)];
[cell.exsInfoLabel setText:spName];
[spName release];

arrEx 由两个项目组成,所以它应该是两个字符串。但是UITableViewCell 只包含第一个字符串。 在 IB 中,我将 UILabel cell.exsInfoLabel 的行数设置为 0。

【问题讨论】:

    标签: ios uikit uilabel


    【解决方案1】:

    试试这个:

    CGSize labelsize;
    UILabel *commentsTextLabel = [[UILabel alloc] init];
    [commentsTextLabel setNumberOfLines:0];
    [commentsTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text = @"yourtextString";
    [commentsTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize = [text sizeWithFont:commentsTextLabel.font 
                 constrainedToSize:CGSizeMake(268, 2000.0) 
                     lineBreakMode:UILineBreakModeWordWrap];
    commentsTextLabel.frame = CGRectMake(10, 24, 268, labelsize.height);
    [cell.contentView addSubview:commentsTextLabel];
    [commentsTextLabel release];
    

    【讨论】:

    • 根据您的要求修改框架。
    • 您好,我尝试将 setNumberOfLines 设置为 0。但它不起作用我们了解您的帖子吗?
    • 行数 0 表示您可以添加无限多行。苹果文档说:-此属性控制要使用的最大行数,以使标签的文本适合其边界矩形。此属性的默认值为 1。要删除任何最大限​​制并根据需要使用尽可能多的行,请将此属性的值设置为 0。
    • 从 iOS6 开始,UILineBreakModeWordWrap 已被弃用。你可以使用NSLineBreakByWordWrapping
    • 如果您正在处理自动布局,您可能需要设置 [label setPreferredMaxLayoutWidth:labelSize.Width]。看到这个答案:stackoverflow.com/questions/12789013/… 我正在使用 Masonry 库来生成自动布局约束,这为我修复了它。
    【解决方案2】:

    在setText之前试试:

    cell.exsInfoLabel.numberOfLines = 2;
    

    或者:

    cell.exsInfoLabel.numberOfLines = arrEx.count;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 2016-06-14
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多