我想支持动态类型,但仅支持某个限制,类似于Settings.app,其中标准UITableViewCell
s可以增长到UIContentSizeCategoryAccessibilityExtraExtraLarge
但不能更大。
有没有一种简单的方法可以使用标准的UITableViewCell样式完成此操作?
我在UIFont上使用自定义类别以获取具有限制的首选字体,像这样
@implementation UIFont (preferredFontWithSizeLimit)
+ (UIFont *)preferredFontWithTextStyle:(UIFontTextStyle)style maxSize:(CGFloat)maxSize {
// Get the descriptor
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle: style];
// Return a font with the minimum size
return [UIFont fontWithDescriptor: fontDescriptor size: MIN(fontDescriptor.pointSize, 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 maxSize = INFINITY;
NSNumber *limit = sizeLimitByStyle[style];
if (limit) {
maxSize = limit.doubleValue;
}
// Return the font
return [UIFont preferredFontWithTextStyle: style maxSize: maxSize];
}
extension UIFont {
static func preferredFont(withTextStyle textStyle: UIFont.TextStyle, maxSize: CGFloat) -> UIFont {
// Get the descriptor
let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)
// Return a font with the minimum size
return UIFont(descriptor: fontDescriptor, size: min(fontDescriptor.pointSize, maxSize))
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句