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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:37:43+00:00 2026-06-04T04:37:43+00:00

Minor quest – looking for a clean way to dynamically set the size of

  • 0

Minor quest – looking for a clean way to dynamically set the size of tables in a resonably generic way (snippets below are from http://pastebin.com/E2pUFRg4).

Now I thought something like

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [items objectAtIndex:[indexPath row]];

CGSize constraint = CGSizeMake(self.view.bounds.size.width-24.f, CGFLOAT_MAX);

UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

CGSize size = [text sizeWithFont:cell.textLabel.font 
               constrainedToSize:constraint 
                   lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height + 12.f, tableView.rowHeight);

return height;
}

would nicely cut it. But unfortunately on the first call to tableView:heightForRowAtIndexPath: the size of the font on the textlabel is set to 0 (the font itself is otherwise setup). It only gets popoulated to something sensible after the first draw.

So I find myself having to change this to something like:

....
UIFont * font = cell.textLabel.font;

if (font.pointSize == 0)
    font = [UIFont systemFontOfSize:20];

CGSize size = [text sizeWithFont:font
               constrainedToSize:constraint 
                   lineBreakMode:UILineBreakModeWordWrap];
.....

which does the trick. But now we suddenly have a hardcoded font size assumption of 20. Which sort of sucks.

So my questions now are

  • Any way to prevent this ? I.e. hardcoding it this much ?

I also guesstimate the X and Y borders/inset at 2×6 vertical and 2×12 horizontal.

  • is there any way to learn this dynamically or from a constant (as to make it as close to the proper defaults on an iPhone and iPad-UIPopover view) ?

And finally:

  • Suggestions for a cleaner way to do this – which also allows for the detailLabel or similar extra fields to be taken into account.

Complete sample at http://pastebin.com/E2pUFRg4.

Thanks,

Dw.

  • 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-04T04:37:44+00:00Added an answer on June 4, 2026 at 4:37 am

    I think you need to more or less “hardcode” your font size anyway. If you get it from interface builder, or from a central constants file – or even in code – it is always somehow hardcoded.

    If you need to distinguish between device types (iPhone, iPad) etc. you can still use different values according to UI requirements.

    I usually set up a central configuration file for values like this. There you could also “hardcode” your standard margin values, as well as the font sizes of other cell labels. It is common practice to calculate as many values dynamically as possible to reduce the number of constants, but to still resort to constants to “anchor” these values.

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

Sidebar

Related Questions

I'm having some minor problems with some animations I'm trying to set up. I
A very minor saving either way but it prompted a mini-debate in the office
I'm having a minor issue dealing with pointers to overloaded member functions in C++.
I have made a minor change to an Excel macro, and although it worked
Im having MSI ,for minor update ,i will be updating only build number with
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
I am having a minor problem with WCF service proxies where the message contains
I have a minor mode. If that mode is active and the user hits
I am experiencing a minor difficulty. I have a webpage, and have created a
I have found a minor problem when i install my application on real android

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.