Created
May 26, 2016 16:46
-
-
Save lukaskollmer/2eba5b88858238b74c1dac90e4c44029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIFont+CustomFont.m | |
// | |
// Created by Lukas Kollmer on 6/13/15. | |
// Copyright (c) 2015 Lukas Kollmer. All rights reserved. | |
// | |
@import UIKit; | |
#import <objc/runtime.h> | |
BOOL UIFontUseCustomSystemFonts() { | |
return true; | |
} | |
@implementation UIFont (SystemFontAdditions) | |
+ (UIFont *)mediumFontOfSize: (CGFloat)fontSize { | |
if (UIFontUseCustomSystemFonts()) { | |
return [UIFont fontWithName:@"Avenir-Medium" size:fontSize]; | |
} else { | |
return [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize]; | |
} | |
} | |
@end | |
/* | |
This is a class that is only used for storing the implementations of the methods that will be swizzled | |
*/ | |
@interface LKSwizzledImplementationStorage : NSObject | |
@property (nonatomic) Method original; | |
@property (nonatomic) Method swizzled; | |
@end | |
@implementation LKSwizzledImplementationStorage | |
@end | |
/** | |
Ignore the warnings for overriding the following methods | |
The clang warning code is "[-Wobjc-protocol-method-implementation]" | |
*/ | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" // Ignore overridden system methods | |
#pragma clang diagnostic ignored "-Wincomplete-implementation" // Ignore missing overridden system methods | |
@implementation UIFont (CustomSystemFont) | |
+ (void)load { | |
[super load]; | |
////////////////////////////// | |
// swizzle the font methods // | |
////////////////////////////// | |
LKSwizzledImplementationStorage *systemFontOfSizeImplementations = [LKSwizzledImplementationStorage new]; | |
LKSwizzledImplementationStorage *boldSystemFontOfSizeImplementations = [LKSwizzledImplementationStorage new]; | |
LKSwizzledImplementationStorage *italicSystemFontOfSizeImplementations = [LKSwizzledImplementationStorage new]; | |
LKSwizzledImplementationStorage *systemFontOfSizeWeightImplementations = [LKSwizzledImplementationStorage new]; | |
// -systemFontofSize | |
systemFontOfSizeImplementations.original = class_getClassMethod(self, @selector(systemFontOfSize:)); | |
systemFontOfSizeImplementations.swizzled = class_getClassMethod(self, @selector(swizzledSystemFontOfSize:)); | |
// -boldSystemFontofSize | |
boldSystemFontOfSizeImplementations.original = class_getClassMethod(self, @selector(boldSystemFontOfSize:)); | |
boldSystemFontOfSizeImplementations.swizzled = class_getClassMethod(self, @selector(swizzledBoldSystemFontOfSize:)); | |
// -italicSystemFontofSize | |
italicSystemFontOfSizeImplementations.original = class_getClassMethod(self, @selector(italicSystemFontOfSize:)); | |
italicSystemFontOfSizeImplementations.swizzled = class_getClassMethod(self, @selector(swizzledItalicSystemFontOfSize:)); | |
// -systemFontofSizeWeight | |
systemFontOfSizeWeightImplementations.original = class_getClassMethod(self, @selector(systemFontOfSize:weight:)); | |
systemFontOfSizeWeightImplementations.swizzled = class_getClassMethod(self, @selector(swizzledSystemFontOfSize:weight:)); | |
NSArray *implementations = @[systemFontOfSizeImplementations, | |
boldSystemFontOfSizeImplementations, | |
italicSystemFontOfSizeImplementations, | |
systemFontOfSizeWeightImplementations | |
]; | |
if (UIFontUseCustomSystemFonts()) { | |
for (LKSwizzledImplementationStorage *swizzledImplementation in implementations) { | |
method_exchangeImplementations(swizzledImplementation.original, swizzledImplementation.swizzled); | |
} | |
} | |
} | |
// Swizzled methods | |
+ (UIFont *)swizzledSystemFontOfSize:(CGFloat)fontSize { | |
NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; | |
if (osVersion.majorVersion < 9) { | |
return [UIFont fontWithName:@"Avenir-Book" size:fontSize]; | |
} else { | |
return [self swizzledSystemFontOfSize:fontSize]; | |
} | |
} | |
+ (UIFont *)swizzledBoldSystemFontOfSize:(CGFloat)fontSize { | |
NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; | |
if (osVersion.majorVersion < 9) { | |
return [UIFont fontWithName:@"Avenir-Heavy" size:fontSize]; | |
} else { | |
return [self swizzledSystemFontOfSize:fontSize]; | |
} | |
} | |
+ (UIFont *)swizzledItalicSystemFontOfSize:(CGFloat)fontSize { | |
NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; | |
if (osVersion.majorVersion < 9) { | |
return [UIFont fontWithName:@"Avenir-BookOblique" size:fontSize]; | |
} else { | |
return [self swizzledSystemFontOfSize:fontSize]; | |
} | |
} | |
+ (UIFont *)swizzledSystemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight { | |
NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; | |
if (osVersion.majorVersion < 9) { | |
return [UIFont fontWithName:@"Avenir-Book" size:fontSize]; | |
} else { | |
return [self swizzledSystemFontOfSize:fontSize]; | |
} | |
} | |
@end | |
#pragma clang diagnostic pop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment