2

I'd like to have several labels one below the other. The are sized with AutoLayout, I'd like to have the font size as big as possible. adjustsFontSizeToFitWidth only works for the width, but the I get the following result

enter image description here

swalkner
  • 16,679
  • 31
  • 123
  • 210
  • I created this [AdjustableLabel](http://stackoverflow.com/a/33657604/1121497), that can adjust text size to the whole frame, not just the width. – Ferran Maylinch Nov 11 '15 at 20:22

2 Answers2

0

You can re-calculate your content height by calling (it may be a little easier to manage by having your layout built as a tableview, if it isn't already):

[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;

I've also had to hard code this for a specific purpose (added as a UIFont category):

+ (int)contentSizeHeightForItem {
    NSString *cat = [[UIApplication sharedApplication] preferredContentSizeCategory];
    if([cat isEqualToString:UIContentSizeCategoryExtraSmall]) {
        return 84;
    } else if([cat isEqualToString:UIContentSizeCategorySmall]) {
        return 86;
    } else if([cat isEqualToString:UIContentSizeCategoryMedium]) {
        return 88;
    } else if([cat isEqualToString:UIContentSizeCategoryLarge]) {
        return 92;
    } else if([cat isEqualToString:UIContentSizeCategoryExtraLarge]) {
        return 94;
    } else if([cat isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
        return 98;
    } else if([cat isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
        return 102;
    }
    return 80;
}
David McGraw
  • 5,157
  • 7
  • 36
  • 36
0

If your font is constant, you can calculate the necessary height of the labels from the font like so:

UIFont *font = [UIFont fontWithName:@"Helvetica" size:18];
UILabel *label = [UILabel new];
[label setFont:font];
[view addSubview:label];

double labelHeight = font.pointSize + fabs(font.descender);
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[label(labelHeight)]"
                                                             options:0
                                                             metrics:@{@"labelHeight" : @(labelHeight)}
                                                               views:@{@"label" : label}]];

This will guarantee that the full height of the font will be taken into account when setting constraints.