Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:48:28+00:00 2026-06-07T16:48:28+00:00

What is the best way to programmatically create a multiline UILabel, add it to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:48:31+00:00Added an answer on June 7, 2026 at 4:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best non-framework (JSP/Servlet only) way to create a form such that
What is the best way to programmatically check if a PDF file is a
Possible Duplicate: Best way to programmatically detect iPad/iPhone hardware For example I want my
I am wondering whats the best way to programmatically make rounded corners for images.
What is the best way to export the eclipse/java preferences to the database programmatically?
I have a UIView subclass that programmatically creates and adds a number of subviews.
I was wondering what would be the best way to create LINQ-to-SQL classes to
In my viewDidLoad method of my main View Controller I programatically create a segmented
Issue : What is the best way to have a comprehensive view of birth/death/mem
What's the best way to programmatically start an application on login for Windows? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.