iosuitableviewsizewithfont

sizeWithFont Is always returning 0


I'm trying to dynamically determine the size each row should be in in table view using sizeWithFont, but it's always 0.0 for some reason. Here is my code:

    - (CGFloat)labelHeight:(NSString *)text {
        CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"DINCond-Medium" size:14]
                                    constrainedToSize:CGSizeMake(218, 100000)
                                        lineBreakMode:NSLineBreakByWordWrapping];

        NSLog(@"Height: %1f", expectedLabelSize.height);

        return expectedLabelSize.height;
    }

I'm not sure why it would return 0 ever.

Any help would be wunderbar. Thanks.


Solution

  • I guess [text sizeWithFont:[UIFont fontWithName:@"DINCond-Medium" size:14] returns nil in your case, because the font can't be found. Make sure to use the font's proper name and also specify it in the Info.plist.

    See this answer for a more detailed description.