Created
March 30, 2016 09:31
-
-
Save andiradulescu/98f1973b8aae76159514e96943fab415 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
#import "UIFont+MNFonts.h" | |
#import <objc/runtime.h> | |
NSString *const FORegularFontName = @"Copperplate"; | |
NSString *const FOBoldFontName = @"Copperplate-Bold"; | |
NSString *const FOItalicFontName = @"Copperplate-Light"; | |
@implementation UIFont (MNFonts) | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" | |
+ (void)replaceClassSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector { | |
Method originalMethod = class_getClassMethod(self, originalSelector); | |
Method modifiedMethod = class_getClassMethod(self, modifiedSelector); | |
method_exchangeImplementations(originalMethod, modifiedMethod); | |
} | |
+ (void)replaceInstanceSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector { | |
Method originalDecoderMethod = class_getInstanceMethod(self, originalSelector); | |
Method modifiedDecoderMethod = class_getInstanceMethod(self, modifiedSelector); | |
method_exchangeImplementations(originalDecoderMethod, modifiedDecoderMethod); | |
} | |
+ (UIFont *)regularFontWithSize:(CGFloat)size | |
{ | |
return [UIFont fontWithName:FORegularFontName size:size]; | |
} | |
+ (UIFont *)boldFontWithSize:(CGFloat)size | |
{ | |
return [UIFont fontWithName:FOBoldFontName size:size]; | |
} | |
+ (UIFont *)italicFontOfSize:(CGFloat)fontSize | |
{ | |
return [UIFont fontWithName:FOItalicFontName size:fontSize]; | |
} | |
- (id)initCustomWithCoder:(NSCoder *)aDecoder { | |
BOOL result = [aDecoder containsValueForKey:@"UIFontDescriptor"]; | |
if (result) { | |
UIFontDescriptor *descriptor = [aDecoder decodeObjectForKey:@"UIFontDescriptor"]; | |
NSString *fontName; | |
if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontRegularUsage"]) { | |
fontName = FORegularFontName; | |
} | |
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontEmphasizedUsage"]) { | |
fontName = FOBoldFontName; | |
} | |
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontObliqueUsage"]) { | |
fontName = FOItalicFontName; | |
} | |
else { | |
fontName = descriptor.fontAttributes[@"NSFontNameAttribute"]; | |
} | |
return [UIFont fontWithName:fontName size:descriptor.pointSize]; | |
} | |
self = [self initCustomWithCoder:aDecoder]; | |
return self; | |
} | |
+ (void)load | |
{ | |
[self replaceClassSelector:@selector(systemFontOfSize:) withSelector:@selector(regularFontWithSize:)]; | |
[self replaceClassSelector:@selector(boldSystemFontOfSize:) withSelector:@selector(boldFontWithSize:)]; | |
[self replaceClassSelector:@selector(italicSystemFontOfSize:) withSelector:@selector(italicFontOfSize:)]; | |
[self replaceInstanceSelector:@selector(initWithCoder:) withSelector:@selector(initCustomWithCoder:)]; | |
} | |
#pragma clang diagnostic pop | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment