【问题标题】:Resizing UILabel inside UITableViewCell to fit content调整 UITableViewCell 中的 UILabel 大小以适应内容
【发布时间】:2014-07-02 00:17:39
【问题描述】:

我已成功调整 UITableViewCell 的大小以适应其内容,但我的 UILabel 未正确调整大小。这就是我调整单元格大小的方式:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *str = @"My really long text";
    CGSize constrainedSize = CGSizeMake(250, 9999);

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"HelveticaNeue-Light" size:16.0], NSFontAttributeName,
                                          nil];

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str attributes:attributesDictionary];

    CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    if (requiredHeight.size.width > 250) {
        requiredHeight = CGRectMake(0,0, 250, requiredHeight.size.height);
    }

    if (requiredHeight.size.height + 10 >= 60)
        return requiredHeight.size.height + 10;
    else
        return 60;
}

我在 Storyboard 的原型单元格中创建了我的 UILabel

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Post-Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILabel *text = (UILabel *)[cell viewWithTag:2];
    text.text = @"My Label's Text";
    [text sizeToFit];

    return cell;
}

【问题讨论】:

  • 为什么我收到了反对票...
  • 您如何将标签绑定到您的单元格?您能否确保当您更新单元格的框架时,内容视图的框架已更新?我通常将我的标签限制在内容视图中,因此更改它的框架会更改标签
  • @AlexReynolds 更新了我的答案,谢谢
  • height 方法中有很多可以改进的代码。 return MAX(60,requiredHeight.size.height + 10)if(requiredHeigh.sie.width 可以删除,因为它什么都不做。你能解释一下问题是什么吗?最简单的方法可能是包含它正在做什么的屏幕截图。
  • 最后,标签是否会超过 1 行文本,因为有更好的方法来测量多行标签

标签: ios objective-c uitableview uilabel


【解决方案1】:

我将它用于我的动态标签。首先,我知道我的标签从左侧被限制在 60 像素以上,从右侧被限制在 67 像素。所以我知道我的标签将在包装之前将屏幕宽度减去填充以适应其内容。无论有多少行,这种方法都会给我title 的高度。我将最低高度设置为44,这样即使用户有超级小文本,我仍然有一个大小合适的单元格。我的单元格中的标签距内容视图的顶部 11 像素,距内容视图的底部 11 像素,因此我将高度添加 22 以进行填充。

+ (CGFloat)cellHeightForTitle:(NSString*)title
{
    UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    NSString *text = title ?: @"test";
    CGFloat hotizontalPadding = 127;
    CGFloat desiredWidth = [UIScreen mainScreen].bounds.size.width - hotizontalPadding;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: font}];

    UILabel *label = [[UILabel alloc] init];

    label.attributedText = attributedText;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [label sizeThatFits:CGSizeMake(desiredWidth, CGFLOAT_MAX)];

    font = nil;
    attributedText = nil;

    return MAX(44, size.height + 22);//top + bottom padding
}

然后在我的桌子上我打电话

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MyCell cellHeightForTitle:@"Some long title"];
}

【讨论】:

  • 这很好用,但你能进一步解释一下 Horizo​​ntalPadding 浮动吗?为什么这会影响单元格的高度。为什么我们使用 CGFLOAT_MAX?谢谢!
  • 在我的单元格中,标签从左侧插入 60 像素,从右侧插入 67n 像素。所以标签只有剩余部分来适应文本。我很确定您的标签不会从 x 0 开始,而是单元格的整个宽度。为了正确测量,您必须使用可用宽度。 |-(60)-标签-(67)-|是我的布局。示例 320 像素的文本将换行在只有 230 宽度的标签中,因此高度会更大。
  • CGFLOAT_MAX 用于您正在测量的事物。在这里我测量高度,所以我在高度中使用它,但如果我在计算宽度,我会把它放在宽度中。我制作了一个矩形,它限制了我们所知道的东西,比如宽度,而不受我们测量的东西的限制,比如高度。您使用的高度大于文本内容的高度,但由于我们不知道内容高度,我们使用 UBER 巨大的数字。如果您需要解释 sizeThatFits 的工作原理,我可以深入探讨。
猜你喜欢
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2014-02-05
  • 1970-01-01
相关资源
最近更新 更多