目前我能想到的修改字体大小的方法有两种情况:
- 要么全体统一修改,可以在展示控件如UILabel 直接加category重写方法或自定义方法,使用runtime动态修改,这种方式不仅可以修改字体大小,还可以修改字体样式。
- 或者是通过为UIFontDescriptor添加category方法实现动态改变指定控件(如UILabel UIButtonLabel等)的字体大小。
下面我介绍的就是第二种通过UIFontDescriptor的修改
项目截图
![gif](https://github.com/Shenjinghao/JHDynamicFont/blob/master/snapshot/DynamicFont.gif)
gif
- 无法查看请点这个 gif图链接
方法介绍
- UIFontDescriptor+JHFontDescriptor.h 核心字体设置类,通过category为UIFontDescriptor添加下列几种方法
+ (UIFontDescriptor *)JH_preferredFontDescriptorWithTextStyle:(UIFontTextStyle)style contentString:(NSString *)contentString;
+ (UIFontDescriptor *)JH_preferredBoldFontDescriptorWithTextStyle:(UIFontTextStyle)style contentString:(NSString *)contentString;
+ (UIFontDescriptor *)JH_fontDescriptorWithSymbolicTraits:(UIFontDescriptorSymbolicTraits)symbolicTraits textStyle:(UIFontTextStyle)style contentString:(NSString *)contentString;
- 方法的一部分实现为
static dispatch_once_t onceToken;
static NSDictionary *fontSizeDict;
dispatch_once(&onceToken, ^{
fontSizeDict = @{
UIFontTextStyleHeadline: @{
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @26,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @25,
UIContentSizeCategoryAccessibilityExtraLarge: @24,
UIContentSizeCategoryAccessibilityLarge: @24,
UIContentSizeCategoryAccessibilityMedium: @23,
UIContentSizeCategoryExtraExtraExtraLarge: @23,
UIContentSizeCategoryExtraExtraLarge: @22,
UIContentSizeCategoryExtraLarge: @21,
UIContentSizeCategoryLarge: @20,
UIContentSizeCategoryMedium: @19,
UIContentSizeCategorySmall: @18,
UIContentSizeCategoryExtraSmall: @17,},
定义一个字典来重新储存需要修改的字体大小,UIFontTextStyleHeadline等等几种文字的类型就不具体介绍了。
- JHDynamicFontManager字体管理类
+ (instancetype)defaultManager
{
static JHDynamicFontManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc] init];
[manager registerDynamicFontNotification];
});
return manager;
}
#pragma mark 添加通知观察者
- (void)registerDynamicFontNotification
{
//当不同类别的字体大小发生变化时接收通知
//// userInfo dictionary will contain new value for UIContentSizeCategoryNewValueKey
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedFontSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
}
添加通知观察者,当有字体修改会受到通知,并将通知传递给我们自己已经定义好的通知
#pragma mark 收到并传递通知
- (void)receivedFontSizeChanged:(NSNotification *)notification
{
// NSString instance with new content size category in userInfo
NSDictionary *dict = notification.userInfo;
[self postDynamicFontNotificationWithContentSizeStr:dict[UIContentSizeCategoryNewValueKey]];
}
#pragma mark 发送
- (void)postDynamicFontNotificationWithContentSizeStr:(NSString *)contentSizeStr
{
[[NSNotificationCenter defaultCenter] postNotificationName:JHFontDidChangeNotification object:nil userInfo:@{JHFontSizeCategoryNewValueKey:contentSizeStr}];
}
- ViewController demo控制器
在viewdidload里面注册通知观察者,并在receivedFontSizeChanged方法里面对受到的通知消息进行处理,我更改的是一行文字的展示效果textLabel是“这一行就是让你看看效果!!!”,截图如下,gif图gif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedFontSizeChanged:) name:JHFontDidChangeNotification object:nil];
- (void)receivedFontSizeChanged:(NSNotification *)notification
{
// NSString instance with new content size category in userInfo
NSDictionary *dict = notification.userInfo;
NSString *contentSizeStr = dict[JHFontSizeCategoryNewValueKey];
UIFontDescriptor *descriptor = [UIFontDescriptor JH_preferredFontDescriptorWithTextStyle:UIFontTextStyleBody contentString:contentSizeStr];
_textLabel.font = [UIFont fontWithDescriptor:descriptor size:0.0];
}
dyna.png
- JHSliderView 滑块的实现类,可以调整字体选项的个数。需要的可以直接查看源码。
喜欢的可以在git上面给个star谢谢!!!