我们正在设计一款适用于iOS7的应用程序,我们的设计师想使用非默认字体(Avenir),但我不想失去动态类型功能。据我所知,动态类型只能与默认系统字体Helvetica Neue一起使用。在此刻,是否可以使用其他字体,还是不可行?
我们正在设计一款适用于iOS7的应用程序,我们的设计师想使用非默认字体(Avenir),但我不想失去动态类型功能。据我所知,动态类型只能与默认系统字体Helvetica Neue一起使用。在此刻,是否可以使用其他字体,还是不可行?
[UIFont preferredFontForTextStyle:]
返回一个特定字体样式的固定大小字体,而不考虑文本视图默认大小。我期望在设置中更改文本大小会对我的应用程序中的文本大小产生一些变化,而不是设置固定值。如iOS文本编程指南所述,由文本样式描述的实际字体可以基于许多动态考虑因素而异,包括用户的内容大小类别偏好,它由UIApplication属性preferredContentSizeCategory表示。preferredContentSizeCategory
会在设置中更改文本大小时发生变化。preferredContentSizeCategory
属性计算大小差值,并将该差值应用于文本视图的默认字体大小(该大小在IB或以编程方式设置)。@interface PreferredFontLabel : UILabel
@property (nonatomic) UIFontDescriptor *defaultFontDescriptor;
@end
PreferredFontLabel.m
#import "PreferredFontLabel.h"
#import "UIApplication+ContentSize.h"
@implementation PreferredFontLabel
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
self.defaultFontDescriptor = self.font.fontDescriptor;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(contentSizeCategoryDidChange)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
[self contentSizeCategoryDidChange];
}
- (void)setDefaultFontDescriptor:(UIFontDescriptor *)defaultFontDescriptor
{
_defaultFontDescriptor = defaultFontDescriptor;
[self contentSizeCategoryDidChange];
}
- (void)contentSizeCategoryDidChange
{
CGFloat preferredSize = [self.defaultFontDescriptor.fontAttributes[UIFontDescriptorSizeAttribute] floatValue];
preferredSize += [UIApplication sharedApplication].contentSizeDelta;
self.font = [UIFont fontWithDescriptor:self.defaultFontDescriptor size:preferredSize];
[self invalidateIntrinsicContentSize];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
}
@end
UIApplication+ContentSize.h
@interface UIApplication (ContentSize)
@property (nonatomic, readonly) NSInteger contentSizeDelta;
@end
UIApplication+ContentSize.m
#import "UIApplication+ContentSize.h"
@implementation UIApplication (ContentSize)
- (NSInteger)contentSizeDelta
{
static NSArray *contentSizeCategories;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
contentSizeCategories = @[UIContentSizeCategoryExtraSmall,
UIContentSizeCategorySmall,
UIContentSizeCategoryMedium,
UIContentSizeCategoryLarge,
UIContentSizeCategoryExtraLarge,
UIContentSizeCategoryExtraExtraLarge,
UIContentSizeCategoryExtraExtraExtraLarge
UIContentSizeCategoryAccessibilityMedium,
UIContentSizeCategoryAccessibilityLarge,
UIContentSizeCategoryAccessibilityExtraLarge,
UIContentSizeCategoryAccessibilityExtraExtraLarge,
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge];
});
// assume UIContentSizeCategoryLarge is default category
NSInteger contentSizeDelta = [contentSizeCategories indexOfObject:self.preferredContentSizeCategory];
if(contentSizeDelta != NSNotFound) {
contentSizeDelta -= [contentSizeCategories indexOfObject:UIContentSizeCategoryLarge];
return contentSizeDelta;
} else {
return 0;
}
}
@end
我增加了属性字符串支持,演示可在GitHub上找到。
使用自定义字体的示例。
https://github.com/jszumski/dynamic-type
请查看@implementation UIFont (AvenirContentSize)
了解如何调整字体以匹配默认大小