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