iOS 7 和 Helvetica Neue UltraLight:用作旧 iOS 版本的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17414630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 7 and Helvetica Neue UltraLight: use as default for older iOS versions
提问by Mobiletainment
To my knowledge, the default font of iOS 7 is Helvetica Neue UltraLight, which is a lot thinner compared to its bold predecessor. To provide a consistent design and make my forthcoming apps look the same across all common iOS versions, I'd like to apply Helvetica Neue UltraLight as the default (primary) font of the app.
据我所知,iOS 7 的默认字体是Helvetica Neue UltraLight,与其粗体的前身相比要细得多。为了提供一致的设计并使我即将推出的应用程序在所有常见的 iOS 版本中看起来都一样,我想应用 Helvetica Neue UltraLight 作为应用程序的默认(主要)字体。
Gladly, this "new font" is available since iOS version 5.0, so it's already supported by versions prior to iOS 7. Sadly, the only way I figured out to use it, is to manually call [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size]
on each UIView's font, which is tedious and error-prone to inconsistency.
很高兴,这种“新字体”自 iOS 5.0 版开始可用,因此 iOS 7 之前的版本已经支持它。遗憾的是,我想出使用它的唯一方法是手动调用[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size]
每个 UIView 的字体,这既乏味又乏味容易产生不一致的错误。
So my question is, what is your way to do this or how do you handle this design change?
所以我的问题是,您是如何做到这一点的,或者您如何处理这种设计变更?
回答by Leo Natan
Here is the Objective-C Runtime solution:
这是 Objective-C 运行时解决方案:
@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
What I do in this example is replace the three system font methods with my own and test to see if the system version is 7 or up. If it is, I use the original methods, otherwise return a font of my choosing (in this case Helvetica Neue with UltraLight weight for regular and italic requests, and Medium weight for bold requests).
我在这个例子中所做的是用我自己的三种系统字体方法替换并测试系统版本是否为7或更高。如果是,我使用原始方法,否则返回我选择的字体(在这种情况下,Helvetica Neue 为常规和斜体请求使用 UltraLight 粗细,粗体请求为中等粗细)。
This works for everything generated in code, including system created views. It does not work when loading views from Xib and Storyboard files, because the fonts are hardcoded in the NIB file itself. Use the font picker to choose the font you need.
这适用于代码中生成的所有内容,包括系统创建的视图。从 Xib 和 Storyboard 文件加载视图时它不起作用,因为字体是硬编码在 NIB 文件本身中的。使用字体选择器选择您需要的字体。
回答by Mark McCorkle
Why not just use the appearance API?
为什么不直接使用外观 API?
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:14.0]];
回答by Adam Eberbach
Without breaking NDA by being specific about the method names - why not declare a category on UIFont to mimic the new methods for dynamic type?
在不通过特定于方法名称的情况下破坏 NDA - 为什么不在 UIFont 上声明一个类别来模仿动态类型的新方法?
回答by Pat
For those looking for a solution in storyboards, I had some success opening the storyboard file in a text editor and doing a search/replace. You have to catch each type of font you want to change (e.g. Bold/Italic), but I was able to replace the standard system font with HelveticaNeue UltraLight by replacing:
对于那些在故事板中寻找解决方案的人,我在文本编辑器中打开故事板文件并进行搜索/替换取得了一些成功。您必须捕获要更改的每种字体类型(例如粗体/斜体),但我能够通过替换以下方式用 HelveticaNeue UltraLight 替换标准系统字体:
key="fontDescription" type="system"
with
和
key="fontDescription" name="HelveticaNeue-UltraLight" family="Helvetica Neue"
For bold, you would replace:
对于粗体,您将替换:
key="fontDescription" type="boldSystem"
I chose to replace my bold fonts with the same UltraLight font, but for those wanting a "bold" treatment you could choose "HelveticaNeue-Thin".
我选择用相同的 UltraLight 字体替换我的粗体字体,但对于那些想要“粗体”处理的人,您可以选择“HelveticaNeue-Thin”。
I recommend using version control or making a backup copy of your file before doing this though!
不过,我建议在执行此操作之前使用版本控制或制作文件的备份副本!