iOS 7和Helvetica Neue UltraLight:用作旧iOS版本的默认值 [英] iOS 7 and Helvetica Neue UltraLight: use as default for older iOS versions

查看:207
本文介绍了iOS 7和Helvetica Neue UltraLight:用作旧iOS版本的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



很高兴,这个新字体可从自iOS版本5.0 开始,所以它是已经支持iOS 7之前的版本。可惜的是,我想出来使用它的唯一方法是手动调用 [UIFont fontWithName:@HelveticaNeue-UltraLightsize:size] 在每个UIView的字体上,这是冗长乏味且容易出现不一致的问题。



所以我的问题是,你的方式是这样做的,或者你如何处理这个设计更改?

解决方案

这是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
{
方法orig = class_getClassMethod([UIFont类],@selector(systemFontOfSize :));
方法swiz = class_getClassMethod([UIFont类],@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 )
{
//调用原始的实现。
return [self ln_systemFontOfSize:fontSize];
}

return [UIFont fontWithName:@HelveticaNeuesize:fontSize];


+(UIFont *)ln_boldSystemFontOfSize :( CGFloat)fontSize
{
if([[UIDevice currentDevice] systemVersion] floatValue]> = 7.0f )
{
//调用原始实现。
return [self ln_systemFontOfSize:fontSize];
}

return [UIFont fontWithName:@HelveticaNeue-Mediumsize:fontSize];


+(UIFont *)ln_italicSystemFontOfSize :( CGFloat)fontSize
{
if([[UIDevice currentDevice] systemVersion] floatValue]> = 7.0f )
{
//调用原始实现。
return [self ln_systemFontOfSize:fontSize];
}

return [UIFont fontWithName:@HelveticaNeue-Italicsize:fontSize];
}

@end

我在这个例子中做了什么用我自己替换三种系统字体方法,并测试系统版本是否为7或更高版本。如果是,我使用原始方法,否则返回一个我选择的字体(在这种情况下,Helvetica Neue具有UltraLight权重,用于常规和斜体请求,中等重量用于粗体请求)。



这适用于代码中生成的所有内容,包括系统创建的视图。从Xib和Storyboard文件加载视图时,它不起作用,因为NIB文件中的字体是硬编码的。使用字体选择器选择您需要的字体。


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.

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.

So my question is, what is your way to do this or how do you handle this design change?

解决方案

Here is the Objective-C Runtime solution:

@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).

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.

这篇关于iOS 7和Helvetica Neue UltraLight:用作旧iOS版本的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆