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 7028033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:19:56+00:00 2026-05-28T00:19:56+00:00

I have a subclass of UILabel, which is supposed to update its text when

  • 0

I have a subclass of UILabel, which is supposed to update its text when the user types something. Naturally, as the length of text increases, the size of the label must adjust to accommodate the text. I called the sizeToFit method, and while the label adjusts its width correctly, the bottom of the text is cut off. The problem is that the text includes subscripts and superscripts , and the label is not adjusting itself with the subscripts in consideration (for example, with H₂O the bottom of the two is cut off).

Can I override sizeToFit or sizeThatFits: to increase the height of the label?

EDIT:

- (void) addCompound {

self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

[self addSubview:self.currentLabel];

[self.currentLabel sizeToFit];

// Right now self.currentlabel.text = "". However, I've confirmed thru NSLogging that letters are added to self.currentLabel.text as the user types on the keyboard. Also, the text displays properly (as long as it's within the original frame) when I remove [sel.currentLabel sizeToFit]

}
  • 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-05-28T00:19:57+00:00Added an answer on May 28, 2026 at 12:19 am

    You should override the UILabel method (CGSize)sizeThatFits:(CGSize)size in your subclass like example below. I just add 10pt to the height calculated by UILabel to accommodate the subscript.

    @implementation ESKLabel
    - (CGSize)sizeThatFits:(CGSize)size
    {
        CGSize theSize = [super sizeThatFits:size];
        return CGSizeMake(theSize.width, theSize.height + 10);
    }
    @end
    

    Sample output:

    self.eskLabel.text = @"Hello Long² Long\u2082 World";
    NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
    [self.eskLabel sizeToFit];
    NSLog(@"CGSize: %@", NSStringFromCGSize(self.eskLabel.frame.size));
    

    From the NSLog:

    This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all Attaching to process 864. 
    2012-01-06 23:34:21.949 Stackoverflow4[864:f803] CGSize: {85, 61} 
    2012-01-06 23:34:21.951 Stackoverflow4[864:f803] CGSize: {302, 44} 
    kill 
    quit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have a subclass of NSWindowController that has its own nib and another class
I have a subclass of UIViewController which handles a UIView. The viewcontroller is presented
so I have a UILabel subclass that only crashes every so often on first
I have a UIViewController subclass. I add many UILabel by using the [self.view addSubview:]
I have a UILabel with a dynamic string as the text property. If I
I have a subclass baz, which needs to be included in both foo and
I have a view that contains a UITableView and a UILabel which works perfectly
I have a subclass of a UILabel that overloads initWithCoder and I was wondering
I have a subclass of UITabBarController which i am using so that i can

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.