Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 and Helvetica Neue UltraLight: use as default for older iOS versions

Tags:

ios

iphone

ios7

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?

like image 215
Mobiletainment Avatar asked Jul 01 '13 22:07

Mobiletainment


2 Answers

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.

like image 123
Léo Natan Avatar answered Oct 08 '22 04:10

Léo Natan


Why not just use the appearance API?

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

UIAppearance API

like image 33
Mark McCorkle Avatar answered Oct 08 '22 05:10

Mark McCorkle