Ort*_*ntz 6 iphone cocoa-touch accessibility uikit ios
我想支持动态类型但仅限于某个限制,类似于Settings.app,其中标准UITableViewCells可以增长UIContentSizeCategoryAccessibilityExtraExtraLarge但不大.
有没有一种简单的方法来实现标准的UITableViewCell样式?
Joh*_*hen 10
我在 UIFont 上使用自定义类别来获得具有限制的首选字体,如下所示
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))
}
}
Run Code Online (Sandbox Code Playgroud)
@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
Run Code Online (Sandbox Code Playgroud)
要根据样式对限制进行硬编码,您可以添加这样的内容(我将每种样式的当前系统默认值放在注释中)
+ (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];
}
Run Code Online (Sandbox Code Playgroud)