What is the best way to programmatically create a multiline UILabel, add it to a UIView, and add that UIView to a view controller so that it will properly resize when the orientation changes?
I see that similar questions have been asked many times before, but I'm not finding any solutions to my particular situation. I have a view to which I'm trying to add a mutli-line UILabel. The code I've come up with adds the label, but it's always the width of the view in landscape mode. Rotating the iPad results in the whole app rotating as expected, except the UILabel is too wide. I'm sure the answer lies in the .autoresizingMask or .sizeToFit or .autoresizesSubviews properties in some combination, but I'm not getting it so far. Here's my code to this point
UIViewController *vc = [[UIViewController alloc] init];
vc.title = @"Welcome";
// tbc is an instance of the tab bar controller that this view will go into
UILabel *copy = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tbc.view.frame.size.width, MAXFLOAT)];
NSString *copyText = @"Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Cras mattis consectetur purus sit amet fermentum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Cras mattis consectetur purus sit amet fermentum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.";
UIFont *copyFont = [UIFont fontWithName:@"HoeflerText-Regular" size:20.0f];
[copy setContentMode:UIViewContentModeScaleAspectFit];
[copy setText:copyText];
[copy setFont:copyFont];
[copy setNumberOfLines:0];
[copy setLineBreakMode:UILineBreakModeWordWrap];
[copy setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin];
CGRect frame = copy.frame;
frame.origin.x = 50;
frame.origin.y = 100;
frame.size.width = tbc.view.frame.size.width;
frame.size.height = tbc.view.frame.size.height - 100;
[copy setFrame:frame];
[copy sizeToFit];
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbc.view.frame.size.width, vc.view.frame.size.height)];
[content setAutoresizesSubviews:YES];
[content addSubview:copy];
[content sizeToFit];
[vc.view setAutoresizesSubviews:YES];
[vc.view setContentMode:UIViewContentModeScaleAspectFit];
[vc.view addSubview:content];
As I said, I'm sure I'm missing something with the .autoresizeMask but I've been beating my head against a wall on this for a couple of days now so I feel like I've tried everything. Any suggestions would be appreciated.
Just add:
[content setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin];
When you are saying:
setContentMode:, you are just telling the view how to resize the subviews IF the bounds change, however, the bounds aren't changing!! (because of the missing autoresizing mask flags)
if vc is a viewController loaded from a NIB , double check the autorsizing options of it's view from the NIB file, otherwise, add autoresizing mask flags to it, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With