效果图
最近项目要求字体大小不能固定, 要追随系统设置, 先上效果图. 第一张, 系统最小字体, 第二张, 苹果最大字体.(注: 真机测试)
普通界面实现
preferredFontForTextStyle: 方法
preferredFontForTextStyle:
方法会根据用户首选字体和传入的文本样式返回对应的UIFont
对象
添加方法:
- (void)setUpFonts {
// preferredFontForTextStyle 方法会根据用户首选字体和传入的文本样式返回对应的 UIFont 对象
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.nameLabel.font = font;
self.phoneLabel.font = font;
self.sexLabel.font = font;
self.nameField.font = font;
self.phoneField.font = font;
self.sexField.font = font;
}
- 如果是用 xib 拖的控件, 不要限定死控件的宽高, 然它们在 pin 菜单等宽即可(等宽美观). 如果限定死宽高, 文本会显示不全, 如图:
然后在视图加载方法中调用此方法:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setUpViewData];
[self setUpFonts];
}
此时按下HOME键, 在通用->辅助功能->更大字体中改变系统显示字体大小. 切回程序, 发现这个视图里需要改变字体大小的控件的字体并没有改变, 这是因为程序并不知道系统更改字体, 我们需要注册一个监听事件告诉程序字体改变了.
注册监听:
// 注册观察者, 监听字体改变
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setUpFonts)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
其中UIContentSizeCategoryDidChangeNotification
可以监听内容尺寸的变化
在dealloc
方法中销毁观察者
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
至此, 重新运行程序, 重复上面的设置字体大小步骤, 字体发生效果图上的改变.
tableView上改变字体
tableView上设置动态字体需要注意 rowHeight 也要动态随字体改变.
效果图
系统最小字体和最大字体!
实现
首先, 我们需要更改自定义 cell 内字体的显示
和普通界面方法一样, 直接上代码
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setUpCellFont {
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.nameLabel.font = font;
self.phoneLabel.font = font;
self.sexLabel.font = font;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self setUpCellFont];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setUpCellFont)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
接下来, 我们需要根据字体来设置 rowHeight 属性
注意, 系统新增的更大字体选项
- (void)setUpTableViewFont {
static NSDictionary *cellHeight;
if (!cellHeight) {
cellHeight = @{UIContentSizeCategoryExtraSmall: @44,// 没有 Accessibility 是普通字体的大小
UIContentSizeCategorySmall: @44,
UIContentSizeCategoryMedium: @44,
UIContentSizeCategoryLarge: @44,
UIContentSizeCategoryExtraLarge: @55,
UIContentSizeCategoryExtraExtraLarge: @65,
UIContentSizeCategoryExtraExtraExtraLarge: @75,
UIContentSizeCategoryAccessibilityMedium: @75, // 注意这个是更大字体的方法
UIContentSizeCategoryAccessibilityLarge: @85,
UIContentSizeCategoryAccessibilityExtraLarge: @95,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @105,
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @115};
}
// Application 单例自带方法判断内容尺寸
NSString *tableViewSize = [[UIApplication sharedApplication] preferredContentSizeCategory];
// 根据尺寸在 cellHeight 字典中选择相应的行高
NSNumber *tableViewRowHeight = cellHeight[tableViewSize];
self.tableView.rowHeight = tableViewRowHeight.floatValue;
[self.tableView reloadData];
}
方法调用, 注册观察者, 销毁观察者:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setUpTableViewFont)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setUpTableViewFont];
}
OK, 真机上运行程序, 调整字体大小
别的情况以此类推即可.