Ios 按word包装UILabel,并以编程方式展开UITableViewCell高度

Ios 按word包装UILabel,并以编程方式展开UITableViewCell高度,ios,objective-c,autolayout,nslayoutconstraint,ios-autolayout,Ios,Objective C,Autolayout,Nslayoutconstraint,Ios Autolayout,我以编程方式创建了布局约束,以在表格单元格中排列UI组件 以下是我正在实施的约束: 单元格的左侧包含nameuview(完成) 单元格右侧包含valueUIView(完成) nameUIView和valueUIView零件的宽度应相等(完成) 高度和宽度nameUIView和valueUIView应为可用空间的100%(完成) nameiview包含一个nameilabel(完成) nameilabel应使用单词换行(需要建议) 如果nameUILabel中的文本太大,UITableViewC

我以编程方式创建了布局约束,以在表格单元格中排列UI组件

以下是我正在实施的约束:

  • 单元格的左侧包含
    nameuview
    (完成)
  • 单元格右侧包含
    valueUIView
    (完成)
  • nameUIView
    valueUIView
    零件的宽度应相等(完成)
  • 高度和宽度
    nameUIView
    valueUIView
    应为可用空间的100%(完成)
  • nameiview
    包含一个
    nameilabel
    (完成)
  • nameilabel
    应使用单词换行(需要建议)
  • 如果
    nameUILabel
    中的文本太大,UITableViewCell单元格的高度应扩大(需要建议)

如何以编程方式实现该布局的换行和高度扩展

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSString *reuseID = reuseIdentifier;

        // The view containing the label (left, red)
        UIView *nameUIView = [[UIView alloc] init];
        self.nameUIView = attributeNameView;
        [self.nameUIView setBackgroundColor:[UIColor redColor]];
        [self.nameUIView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.contentView addSubview:self.nameUIView];

        // The label (green)
        UILabel *nameUILabel = [[UILabel alloc] init];
        self.nameUILabel = nameUILabel;
        [self.nameUILabel setTextColor:[UIColor blackColor]];
        [self.nameUILabel setBackgroundColor:[UIColor greenColor]];
        [self.nameUILabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.nameUILabel setLineBreakMode:NSLineBreakByWordWrapping];
        [self.nameUIView addSubview:self.nameUILabel];

        // The view containing the value (right, blue)
        UIView *valueUIView = [[UIView alloc] init];
        self.valueUIView = valueUIView;
        [self.valueUIView setBackgroundColor:[UIColor blueColor]];
        [self.valueUIView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.contentView addSubview:self.valueUIView];

        NSDictionary *views = NSDictionaryOfVariableBindings(nameUIView, nameUILabel, valueUIView);
        NSArray *constraints;

        // The constraint to align the views horizonzally (1:1) sizing
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameUIView][valueUIView(==nameUIView)]|"
                                                                       options: 0
                                                                       metrics:nil
                                                                         views:views];

        [self.contentView addConstraints:constraints];

        // 100% height for name view
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameUIView]|"
                                                              options: 0
                                                              metrics:nil
                                                                views:views];
        [self.contentView addConstraints:constraints];

        NSLayoutConstraint *constraint;

        // Center name label horizontally
        constraint = [NSLayoutConstraint constraintWithItem:nameUILabel
                                attribute:NSLayoutAttributeCenterX
                                relatedBy:NSLayoutRelationEqual
                                toItem:nameUIView
                                attribute:NSLayoutAttributeCenterX
                                multiplier:1.f constant:0.f];// Not possible via VFL
        [self.contentView addConstraint:constraint];

        // Center name label vertically
        constraint = [NSLayoutConstraint constraintWithItem:nameUILabel
                                                                      attribute:NSLayoutAttributeCenterY
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:nameUIView
                                                                      attribute:NSLayoutAttributeCenterY
                                                                     multiplier:1.f constant:0.f];// Not possible via VFL
        [self.contentView addConstraint:constraint];

        // 100% height for value view
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[valueUIView]|"
                                                              options: 0
                                                              metrics:nil
                                                                views:views];

        [self.contentView addConstraints:constraints];

    }
    return self;
}
  • 逐字换行
  • 添加此附加约束(将标签宽度设置为父视图)确实启用了文字换行:

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[attributeNameLabel(==attributeNameView)]|"
                                                          options: 0
                                                          metrics:nil
                                                            views:views];
    
            [self.contentView addConstraints:constraints];
    
    最后,我必须将文本居中:

    [self.attributeNameLabel setTextAlignment:NSTextAlignmentCenter];
    
  • 基于内容扩展单元格高度
  • 还需要为UITableView设置这些属性

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight =
    
    将包裹superview tame的高度设置为其包含的标签

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameUIView(==nameUILabel)]|"
                                                          options: 0
                                                          metrics:nil
                                                            views:views];
    
    [self.contentView addConstraints:constraints];
    
    最小高度约束(可选)


    UILabel必须有一个受限制的宽度,以允许包装。@DonMag与我在回答中使用的方法相同。如果标签超出单元格高度,如何扩展单元格高度仍然是一个问题。是否设置了
    tableView.rowHeight=UITableViewAutomaticDimension
    并设置了
    tableView.estimatedRowHeight=
    ?是的,我正在视图中执行此操作。然而,它确实帮助我找到了解决办法。很快就会发布。。。
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameUIView(>=50)]|"
                                                          options: 0
                                                          metrics:nil
                                                            views:views];
    
    [self.contentView addConstraints:constraints];