Supported Limits Dynamic Type Font Size

I want to support the Dynamic type , but only to a certain limit, similar to the Settings.app parameter, where the standard one UITableViewCellcan grow to UIContentSizeCategoryAccessibilityExtraExtraLarge, but not more.

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

+5
source share
4 answers

I solved the problem in my custom subclass UITableViewCell:

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
+2
source

I use a custom category on UIFont to get a preferred font with a restriction like this

@implementation UIFont (preferredFontWithSizeLimit)

+ (UIFont *)preferredFontWithTextStyle:(UIFontTextStyle)style maxSize:(CGFloat)maxSize {
    // Get the descriptor
    UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];

    // Return the descriptor if it ok
    if (fontDescriptor.pointSize <= maxSize)
        return fontDescriptor;

    // Return a descriptor for the limit size
    return [UIFont fontWithDescriptor:fontDescriptor size:maxSize];
}

@end

, , - ( )

+ (UIFont *)limitedPreferredFontForTextStyle:(UIFontTextStyle)style {
    // Create a table of size limits once
    static NSDictionary *sizeLimitByStyle;
    static dispatch_once_t once_token;
    dispatch_once(&once_token, ^{
        sizeLimitByStyle = @{
            UIFontTextStyleTitle1: @56, // default 28
            UIFontTextStyleTitle2: @44, // default 22
            UIFontTextStyleTitle3: @40, // default 20
            UIFontTextStyleHeadline: @34, // default 17
            UIFontTextStyleSubheadline: @30, // default 15
            UIFontTextStyleBody: @34, // default 17
            UIFontTextStyleCallout: @32, // default 16
            UIFontTextStyleFootnote: @26, // default 13
            UIFontTextStyleCaption1: @24, // default 12
            UIFontTextStyleCaption2: @22, // default 11
        };
    });

    // Look up the size limit
    CGFloat sizeLimit = INFINITY;
    NSNumber *limit = sizeLimitByStyle[style];
    if (limit)
        sizeLimit = limit.doubleValue;

    // Return the font
    return [UIFont preferredFontWithTextStyle:style sizeLimit:sizeLimit];
}

4+

extension UIFont {

  static func preferredFont(withTextStyle textStyle: UIFont.TextStyle, maxSize: CGFloat) -> UIFont {
    // Get font descriptor
    let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)

    // Font from descriptor & font with max size
    let font = UIFont(descriptor: fontDescriptor, size: fontDescriptor.pointSize)
    let maxFont = UIFont(descriptor: fontDescriptor, size: maxSize)

    return fontDescriptor.pointSize <= maxSize ? font : maxFont
  }

}
+2

, UIKit. .

  • - [UIFont gvc_preferredFontForTextStyle:]

UIContentSizeCategoryExtraExtraExtraLarge .

+ (UIFont *)gvc_preferredFontForTextStyle:(NSString *)style {
 static dispatch_once_t onceToken;
 static NSDictionary *fontSizeTable;
 dispatch_once(&onceToken, ^{
   fontSizeTable = @{
     UIFontTextStyleBody: @{
      UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @23,
      UIContentSizeCategoryAccessibilityExtraExtraLarge: @23,
      UIContentSizeCategoryAccessibilityExtraLarge: @23,
      UIContentSizeCategoryAccessibilityLarge: @23,
      UIContentSizeCategoryAccessibilityMedium: @23,
      UIContentSizeCategoryExtraExtraExtraLarge: @23,
      UIContentSizeCategoryExtraExtraLarge: @21,
      UIContentSizeCategoryExtraLarge: @19,
      UIContentSizeCategoryLarge: @17,
      UIContentSizeCategoryMedium: @16,
      UIContentSizeCategorySmall: @15,
      UIContentSizeCategoryExtraSmall: @14,},

 };
 });


 NSString *contentSize = [UIApplication sharedApplication].preferredContentSizeCategory;
 CGFloat fontSize = ((NSNumber *)fontSizeTable[style][contentSize]).floatValue;
 return [UIFont systemFontOfSize:fontSize];
  1. [UIFont gvc_preferredFontForTextStyle:]

cell.font = [UIFont gvc_preferredFontForTextStyle:UIFontTextStyleBody]

+1

4+ .

extension UIFont {

  func preferredFont(withTextStyle textStyle: UIFont.TextStyle, maxSize: CGFloat) -> UIFont {
    // Get font descriptor
    let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)

    // Font from descriptor & font with max size
    let font = UIFont(descriptor: fontDescriptor, size: fontDescriptor.pointSize)
    let maxFont = UIFont(descriptor: fontDescriptor, size: maxSize)

    return fontDescriptor.pointSize <= maxSize ? font : maxFont
  }

}
0

All Articles