Ortwin Gentz Ortwin Gentz - 1 month ago 8
iOS Question

Limit supported Dynamic Type font sizes

I want to support Dynamic Type but only to a certain limit, similar to the Settings.app where standard

UITableViewCell
s can grow up to
UIContentSizeCategoryAccessibilityExtraExtraLarge
but not larger.

Is there an easy way to accomplish this with standard UITableViewCell styles?

Answer

I solved the issue in my custom UITableViewCell subclass:

CGFloat const ftMaximumDynamicFontSize = 23;

@implementation MyTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        [self adjustFonts];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self adjustFonts];
}

- (void)adjustFonts {
    UIFont *textFont = self.textLabel.font;
    self.textLabel.font = [UIFont fontWithName:textFont.fontName size:MIN(textFont.pointSize, ftMaximumDynamicFontSize)];

    UIFont *detailFont = self.detailTextLabel.font;
    self.detailTextLabel.font = [UIFont fontWithName:detailFont.fontName size:MIN(detailFont.pointSize, ftMaximumDynamicFontSize)];
}

@end
Comments