iOS7:我们可以使用除dynamictypes以外的Helvetica Neue字体吗?

我们正在为iOS7devise一个应用程序,我们的devise人员希望使用非默认字体(Avenir),但我不想松动dynamictypesfunction。 据我所知dynamictypes只能使用默认的系统字体,这是Helvetica Neue。 是否有可能使用其他字体或在这个时候它不是一个选项?

据我[UIFont preferredFontForTextStyle:]返回一个固定大小的字体为特定的字体风格,无论文本视图的默认大小。 我期望在“设置”中更改文字大小可以改变我应用中的文字大小,而不是设置固定值。 正如iOS文本编程指南所述 ,

用于文本样式所描述的目的的实际字体可以根据许多dynamic考虑事项而变化,包括用户的内容大小类别首选项,其由UIApplication属性preferredContentSizeCategory表示。

我注意到属性preferredContentSizeCategory响应在设置中设置文本大小的变化。

观察UIContentSizeCategoryDidChangeNotification也很重要,以便您可以在用户更改内容大小类别时重新排列文本。 当您的应用程序收到该通知时,应该将invalidateIntrinsicContentSize消息发送到由自动布局定位的视图,或将setNeedsLayout发送到手动定位的用户界面元素。 它应该使首选的字体或字体描述符无效,并根据需要获取新的字体。

因此,我的想法是观察适当的通知,基于preferredContentSizeCategory属性计算大小增量,并将增量应用于文本视图的默认字体大小(在IB中设置或以编程方式)。


PreferredFontLabel.h

 @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 

我添加了属性string支持,演示在GitHub上可用

自定义字体的例子。

https://github.com/jszumski/dynamic-type

请参阅@implementation UIFont (AvenirContentSize)

如何调整字体以匹配默认大小