在iOS7.0 之后我们可以使用UIFont的preferredFontForTextStyle:类方法来指定一个样式,并让字体大小符合用户设定的文字字体大小,目前可供选择的有十种样式:
- UIFontTextStyleTitle1 NS_AVAILABLE_IOS(9_0);
- UIFontTextStyleTitle2 NS_AVAILABLE_IOS(9_0);
- UIFontTextStyleTitle3 NS_AVAILABLE_IOS(9_0);
- UIFontTextStyleHeadline NS_AVAILABLE_IOS(7_0);
- UIFontTextStyleSubheadline NS_AVAILABLE_IOS(7_0);
- UIFontTextStyleBody NS_AVAILABLE_IOS(7_0);
- UIFontTextStyleCallout NS_AVAILABLE_IOS(9_0);
- UIFontTextStyleFootnote NS_AVAILABLE_IOS(7_0);
- UIFontTextStyleCaption1 NS_AVAILABLE_IOS(7_0);
-
UIFontTextStyleCaption2 NS_AVAILABLE_IOS(7_0);
当在设置里更改时,如果自己的代码中有用到UIFont.preferredFontForTextStyle 的,给所在的View或VC添加UIContentSizeCategoryDidChangeNotification通知,方法里重新设置一下font即可。即:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
设置字体代码:
- (void)contentSizeCategoryChanged:(NSNotification *)notification{
self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
}