我以编程方式创建了布局约束,以便在表格单元格中排列UI组件。
以下是我正在实施的约束:
nameUIView
(已完成)valueUIView
(已完成)nameUIView
和valueUIView
部分的宽度应该相等(已完成)nameUIView
和valueUIView
应为可用空间的100%(已完成)nameUIView
包含nameUILabel
(已完成)nameUILabel
应该使用换行(需要建议)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;
}
答案 0 :(得分:0)
添加此附加约束(将标签的宽度设置为父视图)确实启用了自动换行:
template <typename T>
void Combination(const std::vector<T>& v, std::size_t count)
{
assert(count <= v.size());
std::vector<bool> bitset(v.size() - count, 0);
bitset.resize(v.size(), 1);
do {
for (std::size_t i = 0; i != v.size(); ++i) {
if (bitset[i]) {
std::cout << v[i] << " ";
}
}
std::cout << std::endl;
} while (std::next_permutation(bitset.begin(), bitset.end()));
}
最后,我不得不把文字集中在一起:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[attributeNameLabel(==attributeNameView)]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
还需要为UITableView
设置这些属性[self.attributeNameLabel setTextAlignment:NSTextAlignmentCenter];
将包装superview tame的高度设置为它包含的标签
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight =
最小高度限制(可选)
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameUIView(==nameUILabel)]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];