ios - 调整 UITableViewCell 内的 UILabel 以适应内容

标签 ios objective-c uitableview uilabel

我已经成功地调整了我的 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 的原型(prototype)单元格中创建了 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;
}

最佳答案

我将它用于我的动态标签。首先我知道我的标签被限制在左边超过 60px 和右边超过 67px。所以我知道我的标签在换行前将屏幕宽度减去填充以适合其内容。无论有多少行,此方法都会给我 title 的高度。我将最低高度设置为 44,这样即使用户使用超小文本,我仍然有一个大小合适的单元格。我的单元格中的标签距离内容 View 顶部 11 像素,距离内容 View 底部 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"];
}

关于ios - 调整 UITableViewCell 内的 UILabel 以适应内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493926/

相关文章:

iOS——如何用左上角缩放 UIView?

ios - 在 Swift 中打乱一个字符串

objective-c - Objective C - 类别和继承 - 将方法添加到基类并将覆盖添加到派生类

ios - 检测 UITableView 部分标题是否 float

ios - 将 TableView 添加为 subview 不会重新加载数据

ios - 如何避免让每个 viewController 成为 adWhirl 委托(delegate)? (是否有 AdWhirl 设计模式)

ios - Sqlite 更新命令不起作用

ios - 收到推送通知时执行代码

objective-c - 通过RubyCocoa调用外部shell应用程序

iphone - 在 iPhone 的 Objective-C 中的 (commitEditingStyle) 之外调用 (deleteRowsAtIndexPaths)