0

I'm facing this problem:

I have a tableView and a customCell, on my custom cell class I set font size and font colors for the labels that I have there, my issue is that in order to calculate the heightForRowAtIndexPath I don't have access to the labels font sizes, what I do in that method create temporary labels with the same text value that I set on the custom cell class and calculate the label height in order to set the heightForRowAtIndexPath height depending on the content but since I don't have the correct font it gives me a different height and I see spaces between each cell, is there any way to solve this?

user9898
  • 25
  • 3
  • Can you use different font size for each label instead of change the heigh of each row? – Nekak Kinich Jun 09 '15 at 20:09
  • 1
    If using iOS8 only, you could use auto sizing. If you are supporting pre iOS8, then create a prototype cell. See http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930 – Rory McKinnel Jun 09 '15 at 20:19

1 Answers1

0

Why not create global macros for the different font sizes.

#define LABEL1_FONT [UIFont fontWithName:@"Helvetica Neue" size:17]

Then you can make the check for the height:

UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font =LABEL1_FONT;
    gettingSizeLabel.text = yourText;
    CGSize maximumLabelSize = CGSizeMake(291, 9999);

    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

    // Get height required for label title
    float lineHeightInfo = expectedSize.height / LABEL1_FONT.lineHeight;
sublimepremise
  • 245
  • 1
  • 2
  • 10
  • I can't set a global macros for the font sizes, right now on my project I'm using UI_APPEARANCE_SELECTOR on my custom cell class, in there I set the fonts and styles for the cell labels, at that level I do have the correct sizes so on the layoutSubviews method I set the frames, but at mi table view controller label, on the heightForRowAtIndex delegate I create temporary labels with the same content as the one is set on the cell class, but the difference is that I don't have access to the label font size, I was wondering if there is a chance to fire the heightForRow when everything is in place – user9898 Jun 10 '15 at 02:57