我想支持Dynamic Type,但仅限于某个限制,类似于Settings.app,其中标准UITableViewCell
可以增长到UIContentSizeCategoryAccessibilityExtraExtraLarge
但不会更大。
使用标准UITableViewCell样式有没有简单的方法来实现这一目标?
答案 0 :(得分:2)
我在自定义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
答案 1 :(得分:2)
我在UIFont上使用自定义类别来获取具有限制的首选字体,例如
@implementation UIFont (preferredFontWithSizeLimit)
+ (UIFont *)preferredFontWithTextStyle:(UIFontTextStyle)style maxSize:(CGFloat)maxSize {
// Get the descriptor
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:style];
// Return the descriptor if it's 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];
}
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 :(得分:1)
我找不到使用现有UIKit方法的任何解决方案。但是有一种简单的方法可以实现这一目标。
[UIFont gvc_preferredFontForTextStyle:]
此处字体大小增加到UIContentSizeCategoryExtraExtraExtraLarge
并且在后续字词之后保持不变。 Related question
+ (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];
[UIFont gvc_preferredFontForTextStyle:]
设置代码字体 cell.font = [UIFont gvc_preferredFontForTextStyle:UIFontTextStyleBody]