据我所知,所有标准控件默认使用系统字体。
此外,API [UIFont systemFontOfSize:]也使用系统字体。
有没有办法为整个应用程序重新定义它,而不是为表格,标签等设置字体?
答案 0 :(得分:9)
答案是否定的,你无法改变苹果的系统,你需要自己在控件上设置字体。
要为整个iOS应用设置默认字体的最佳方式,请查看以下SO问题:
Set a default font for whole iOS app?
How to set a custom font for entire iOS app without specifying size
How do I set a custom font for the whole application?
Is there a simple way to set a default font for the whole app?
答案 1 :(得分:5)
定义并导出(通过在文件或预编译头中包含标题)UIFont
类别,如下所示:
@implementation UIFont (Utils)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)systemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:@"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)lightSystemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:@"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)boldSystemFontOfSize:(CGFloat)size
{
return [UIFont fontWithName:@"YOUR_TRUETYPE_FONT_NAME_HERE" size:size];
}
+ (UIFont *)preferredFontForTextStyle:(NSString *)style
{
if ([style isEqualToString:UIFontTextStyleBody])
return [UIFont systemFontOfSize:17];
if ([style isEqualToString:UIFontTextStyleHeadline])
return [UIFont boldSystemFontOfSize:17];
if ([style isEqualToString:UIFontTextStyleSubheadline])
return [UIFont systemFontOfSize:15];
if ([style isEqualToString:UIFontTextStyleFootnote])
return [UIFont systemFontOfSize:13];
if ([style isEqualToString:UIFontTextStyleCaption1])
return [UIFont systemFontOfSize:12];
if ([style isEqualToString:UIFontTextStyleCaption2])
return [UIFont systemFontOfSize:11];
return [UIFont systemFontOfSize:17];
}
#pragma clang diagnostic pop
@end
从iOS 7开始,Light变体是一个有用的额外功能。享受吧! ;)