プログラムでコンテンツが取り込まれた UIView ウィッチが 1 つあります。この UIView のレイアウトは Auto-layout を使用して作成されます。
このビューは、1 つの UIScrollView のサブビューである必要があり、垂直方向にスクロールする必要があります。
次のようなコードがあります。
// self.scrollview is defined in IB and it has constraints to edges (margin:0 0 0 0)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, MAXFLOAT)];
[contentView setTranslatesAutoresizingMaskIntoConstraints:YES];
// Then I call some my parser which add subviews to the contentView and layout them using constraints.
[self.scrollview addSubview:contentView];
[contentView setNeedsLayout];
[contentView layoutIfNeeded];
// Now, I need to know height of the contentView to be able to set contentSize to self.scrollview
//更新
OK、この UILabel を self.content に追加しました。これは UIView で作成されたものです。[[UIView alloc] initWithFrame:CGRectMake(self.layoutMargin, self.layoutMargin, 1024 - self.layoutMargin*2, 100)]);
この self.content ビューは、私にとって単なるコンテナであり、いくつかの に配置する必要がありますUIScrollView
。の contentSize を設定するには、self.content の高さが必要ですUIScrollView
。
[self.content setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.content setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.content setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
UILabel *textlabel = [[UILabel alloc] init];
UIFont *font;
font = [UIFont fontWithName:@"InterstatePlus-Regular" size:16];
UIFont *bold;
bold = [UIFont fontWithName:@"InterstatePlus-Bold" size:16];
NSDictionary *style = @{
@"$default" : @{NSFontAttributeName : font},
@"b" : @{NSFontAttributeName : bold},
@"em" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Italic" size:14]},
@"h1" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:48]},
@"h2" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:36]},
@"h3" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:32]},
@"h4" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:24]},
@"h5" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:18]},
@"h6" : @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:16]}
};
[textlabel setNumberOfLines:0];
NSError *error = nil;
NSString *string = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing <br>Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type<br> and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
NSString *replacedString = [string stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
NSAttributedString *attributedString = [SLSMarkupParser attributedStringWithMarkup:replacedString style:style error:&error];
NSMutableAttributedString *mutAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
NSInteger strLength = [attributedString length];
NSMutableParagraphStyle *paragraphstyle = [[NSMutableParagraphStyle alloc] init];
[mutAttributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphstyle
range:NSMakeRange(0, strLength)];
if (mutAttributedString) {
textlabel.attributedText = mutAttributedString;
}
[textlabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[textlabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[textlabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
textlabel.textAlignment = NSTextAlignmentLeft;
[textlabel setTextColor:[UIColor blackColor]];
[textlabel setBackgroundColor:[UIColor clearColor]];
textlabel.lineBreakMode = NSLineBreakByWordWrapping;
[textlabel setNumberOfLines:0];
[self.content addSubview:textlabel];
[textlabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.content.mas_top).with.offset(0);
make.right.equalTo(self.content.mas_right).with.offset(0);
make.height.equalTo(@1200);
make.left.equalTo(self.content.mas_left).with.offset(0);
}];
[self.containerView addSubview:self.content];
[self.containerView setBackgroundColor:[UIColor redColor]];
[self.content setBackgroundColor:[UIColor yellowColor]];
CGSize s = [self.content systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"Size: %f %f", s.width, s.height);
これはログです: サイズ: 17713.000000 0.000000
次のようになります: http://d.pr/i/Rxku