iOS 7和Helvetica Neue UltraLight:作为iOS版本的默认版本

据我所知,iOS 7的默认字体是Helvetica Neue UltraLight ,与前作相比,它更薄。 为了提供一致的devise,并使我即将到来的应用程序在所有常见的iOS版本中看起来相同,我想将Helvetica Neue UltraLight作为应用程序的默认(主要)字体。

令人欣慰的是,这个“新的字体”是从iOS 5.0版本开始提供的,所以它已经被iOS 7之前的版本所支持。不幸的是,我唯一想到的方法就是手动调用[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size]每个UIView字体上的[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size] ,这是乏味和容易出错的。

所以我的问题是,你怎么做到这一点,或者你如何处理这个devise变更?

这里是Objective-C运行时解决scheme:

 @interface UIFont (CustomSystemFont) + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize; + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize; @end @implementation UIFont (CustomSystemFont) + (void)load { Method orig = class_getClassMethod([UIFont class], @selector(systemFontOfSize:)); Method swiz = class_getClassMethod([UIFont class], @selector(ln_systemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(boldSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_boldSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); orig = class_getClassMethod([UIFont class], @selector(italicSystemFontOfSize:)); swiz = class_getClassMethod([UIFont class], @selector(ln_italicSystemFontOfSize:)); method_exchangeImplementations(orig, swiz); } + (UIFont *)ln_systemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue" size:fontSize]; } + (UIFont *)ln_boldSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize]; } + (UIFont *)ln_italicSystemFontOfSize:(CGFloat)fontSize { if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) { //Call original implementation. return [self ln_systemFontOfSize:fontSize]; } return [UIFont fontWithName:@"HelveticaNeue-Italic" size:fontSize]; } @end 

我在这个例子中做的是用我自己的replace三种系统字体方法,并testing系统版本是7或以上。 如果是,我使用原始方法,否则返回一个我select的字体(在这种情况下Helvetica Neue的UltraLight体重为正常和斜体的请求,中等体重的粗体请求)。

这适用于在代码中生成的所有内容,包括系统创build的视图。 从Xib和Storyboard文件加载视图时,它不起作用,因为字体在NIB文件本身中被硬编码。 使用字体select器来select你需要的字体。

为什么不使用外观API?

  [[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:14.0]]; 

UIAppearance API

没有通过具体的方法名称来破坏NDA,为什么不在UIFont上声明一个类来模仿dynamictypes的新方法呢?

对于那些在故事板上寻找解决scheme的人来说,我已经成功地在文本编辑器中打开故事板文件并进行search/replace。 你必须抓住你想要改变的每一种字体(例如粗体/斜体),但是我能够用HelveticaNeue UltraLightreplace标准系统字体,

 key="fontDescription" type="system" 

 key="fontDescription" name="HelveticaNeue-UltraLight" family="Helvetica Neue" 

为了大胆,你会取代:

 key="fontDescription" type="boldSystem" 

我select用相同的UltraLight字体replace我的大胆的字体,但对于那些想要“大胆”的处理,你可以select“HelveticaNeue-Thin”。

我build议使用版本控制或在做这个之前做一个文件的备份副本!