Mam jeden UIView czarownica jest wypełniona programowo. Układ w tym UIView odbywa się za pomocą automatycznego układu.UIView z Auto layout, uzyskać jego rozmiar
Ten widok musi być podzbiorem jednego UIScrollView i musi przewijać się w pionie.
mam kod jak poniżej:
// 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
// UPDATE
Ok, mam ten UILabel, dodany do self.content który jest tworzony z UIView: [[UIView alloc] initWithFrame:CGRectMake(self.layoutMargin, self.layoutMargin, 1024 - self.layoutMargin*2, 100)]);
This Widok self.content jest dla mnie tylko kontenerem, który należy umieścić pod numerem UIScrollView
. Potrzebuję wysokości self.content do ustawienia contentSize z 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);
To Log: Rozmiar: 17713,000000 0,000000
I wygląda na to: http://d.pr/i/Rxku
Czy cały ten kod w 'viewDidLoad'? –
Tak, w viewDidLoad – dormitkon