OGeek|极客世界-中国程序员成长平台

标题: ios - 在 iOS 8 中的单个 UITableViewCell 上有 2 个多行标签 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:59
标题: ios - 在 iOS 8 中的单个 UITableViewCell 上有 2 个多行标签

我有一个场景,其中一个 UITableViewCell 有 2 个 UILable。我可以在其中包含来自服务器的长文本,这会导致两个标签同时为多行。

我已经实现了自动布局和

label.numberofline = 0;
tableVieww.estimatedRowHeight  = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;

但它只会扩展一个标签,而不是同时扩展两个标签。请帮忙。

提前致谢



Best Answer-推荐答案


试试这个:

使用此代码计算行高

注意:ExtraHeight"H"

假设您有高度为 100 的单元格,并且该单元格中的标签高度为 20,而额外高度将为 80,因为此代码正在根据文本及其大小计算标签的高度。

你将把这段代码放在 heightforRowatindexpath 中,所以 cell.label 无法到达那里,所以只需传递文本并使用 Method-2强>

方法 - 1:

-(CGFloat)setRowHeightWithLabelUILabel *)lbl withLabelTextNSString *)lblText withExtraHeightCGFloat)H withFontCGFloat)fontSize
{
   CGFloat totalHeight;
   CGFloat lblWidth = lbl.frame.size.width;

   CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           optionsNSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

   if (lblTextSize.height > lbl.frame.size.height) {
    totalHeight = (lblTextSize.height+H);
   }
   else
   {
       totalHeight = lbl.frame.size.height+H;
   }

   NSLog(@"+++++++++++++Height - %.2f",totalHeight);

   return totalHeight;
}

heightforRow 方法中使用它

方法 - 2:

-(CGFloat)setRowHeightWithLabelTextNSString *)lblText withExtraHeightCGFloat)H withFontCGFloat)fontSize
  {
     CGFloat totalHeight;
     CGFloat lblWidth = 300;

     CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
                                           optionsNSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                        attributes{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;

    if (lblTextSize.height > 20) {
        totalHeight = (lblTextSize.height+H);
    }
    else
    {
        totalHeight = 20+H;
    }

    NSLog(@"+++++++++++++Height - %.2f",totalHeight);

    return totalHeight;
}

关于ios - 在 iOS 8 中的单个 UITableViewCell 上有 2 个多行标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008857/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4