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

  • SEARCH
  • Home
  • 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 8319835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:26:48+00:00 2026-06-08T22:26:48+00:00

I want to create UILabel and some other elements with code. UILabel size can

  • 0

I want to create UILabel and some other elements with code. UILabel size can change depending on the text, so I must move other elements accordingly. How can I do that most effectively? I try to access bounds property, but strangely, it is null:

CGRect textRect = CGRectMake(20, 20, 280, 50);
label = [[UILabel alloc] initWithFrame:textRect];
label.text = someString;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;    
[superView addSubview: label];
NSLog(@"bounds: %@", label.bounds);

prints out “bounds: (null)”

The label itself renders just fine in the expected coordinates.

  • 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-08T22:26:50+00:00Added an answer on June 8, 2026 at 10:26 pm

    The bounds property is a CGRect. A CGRect is not an Objective-C object, so you can’t print it directly using %@. You need to convert it to a string first using NSStringFromCGRect:

    NSLog(@"bounds: %@", NSStringFromCGRect(label.bounds));
    

    The best way to handle laying out the UILabel and the elements around it is by creating a custom UIView subclass to contain the label and other elements, and overriding its layoutSubviews method to do the layout.

    For example, let’s say we want to have the label and an image view, and we want the image view to be flush against the bottom edge of the label. We can create a UIView subclass named ContainerView:

    @interface ContainerView : UIView
    
    @property (nonatomic, strong) UILabel *label;
    @property (nonatomic, strong) UIImageView *imageView;
    
    @end
    

    We can use this as the superview of the label and an image view. We’ll rely on the ContainerView to lay out the label and the image, so we don’t need to specify their frames. We’ll also rely on the ContainerView to add the label and the image view as subviews.

    ContainerView *superView = [[ContainerView alloc] initWithFrame:someRect];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = someString;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    superView.label = label;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];
    superView.imageView = imageView;
    
    [self.view addSubview:superView];
    

    Here’s how we implement ContainerView (assuming we’re using ARC):

    @implementation ContainerView
    
    @synthesize label = _label;
    @synthesize imageView = _imageView;
    

    We’ll override ContainerView’s ‘label’ setter to add the label as a subview:

    - (void)setLabel:(UILabel *)label {
        if (_label) {
             [_label removeFromSuperview];
        }
        _label = label;
        if (_label) {
            [self addSubview:label];
        }
    }
    

    We do the same for the ‘imageView’ setter:

    - (void)setImageView:(UIImageView *)imageView {
        if (_imageView) {
             [_imageView removeFromSuperview];
        }
        _imageView = imageView;
        if (_imageView) {
            [self addSubview:imageView];
        }
    }
    

    The interesting part is the layoutSubviews method. The system sends the layoutSubviews message to a view at various times, including after it gets new subviews and when its size changes.

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        // Put the label's upper left corner at (20, 20) and make it as wide as I am,
        // minus a 20 point margin on each side.
        self.label.frame = CGRectMake(20, 20, self.bounds.size.width - 40, 10000);
        // Tell the label to make itself exactly large enough to fit its text.  This will
        // shrink its height.
        [self.label sizeToFit];
    
        // Now put the image view flush against the bottom edge of the label.
        CGSize imageSize = self.imageView.image.size;
        self.imageView.frame = CGRectMake(20, CGRectGetMaxY(self.label.frame),
            imageSize.width, imageSize.height);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a text display area (either UILabel or UITextView) in my
I want to create like following Sample text in UILabel.. Muthu: Hi I am
I want create an application with animate button? how can i do? after click
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want create a excel with Apache POI in java and I must insert
I want to create a UILabel and set its background to the gradient-blue like
I want to create custom labels using UILabel that look like the ones iPhone
i want create multiple search where statement $where_search is a multiple condition from post
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that

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.