如何为superview设置高度约束,其中superview的高度是其子视图的总高度,其子视图可以是多行UILabel的集合(可根据字体大小改变高度)或者显示了多少行文本。)
答案 0 :(得分:1)
你是通过将第一个标签的顶部限制在自己的顶部然后将最后一个标签的底部限制在自己的底部来实现的。中间的每个标签的顶部都限制在之前标签的底部。
@interface SomeView: UIView
@end
@implementation SomeView
- (instancetype)init {
self = [super init];
if (!self)
return nil;
// create an array of random labels
NSArray <UILabel*> *labels = [self labelsWithRandomText:10];
// add each label to the view, setting it's top to the previous view's bottom
UIView *previousView = self;
for (UILabel *label in labels) {
[self addSubview:label];
// bound the left and right of the label to match the parent view
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
// if this is the first label being added, set its top to self's top
// otherwise set its top to the bottom of the previous view
NSLayoutAttribute previousAttribute = previousView == self ? NSLayoutAttributeTop : NSLayoutAttributeBottom;
[self addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:previousAttribute multiplier:1 constant:0]];
// update previous view
previousView = label;
}
// constrain self's bottom to the bottom of the last label
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
return self;
}
// create an array of labels w/ random color and background text
- (NSArray <UILabel *> *)labelsWithRandomText:(int)labelCount {
NSMutableArray <UILabel*> *labels = [NSMutableArray new];
for (int i = 0; i < labelCount; i++) {
UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.numberOfLines = 0;
label.backgroundColor = [self randomColor];
label.text = [self randomString];
[labels addObject:label];
}
return labels;
}
// create random color
- (UIColor *)randomColor {
return [UIColor colorWithRed:(arc4random()%255)/255. green:(arc4random()%255)/255. blue:(arc4random()%255)/255. alpha:1];
}
// create random string
- (NSString *)randomString {
static NSArray <NSString*> *words;
static NSInteger wordCount;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
words = @[@"cat ", @"dog ", @"bird ", @"went ", @"home ", @"with ", @"under ", @"red ", @"blue ", @"green "];
wordCount = [words count];
});
NSMutableString *randomString = [NSMutableString new];
// create somewhere between 10-30 words
int randomWordCount = arc4random() % 20 + 10;
for (int i = 0; i < randomWordCount; i++) {
[randomString appendString:words[arc4random() % wordCount]];
}
return randomString;
}
@end