Skip to content

Commit

Permalink
Framework compatibility
Browse files Browse the repository at this point in the history
OpenSans now works when it becomes a framework with CocoaPods 0.36.

Two steps were necessary (based on [CocoaPods blog post](http://blog.cocoapods.org/Pod-Authors-Guide-to-CocoaPods-Frameworks/):
- Used a system import (`<OpenSans/UIFont+OpenSans.h>`) to import the header
- Created a private class to load the font, as `NSBundle +mainBundle` needs to be replaced with `NSBundle +bundleForClass:`, but couldn't do it from a category.
  • Loading branch information
marcelofabri committed Dec 31, 2014
1 parent ce02d43 commit 874e65b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions UIFont+OpenSans.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
//

#import <CoreText/CoreText.h>
#import "UIFont+OpenSans.h"
#import <OpenSans/UIFont+OpenSans.h>

@interface KOSFontLoader : NSObject

@implementation UIFont (OpenSans)
+ (void)loadFontWithName:(NSString *)fontName;

@end

void KOSLoadFontWithName(NSString *fontName) {
NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"OpenSans" withExtension:@"bundle"];
@implementation KOSFontLoader

+ (void)loadFontWithName:(NSString *)fontName {
NSURL *bundleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"OpenSans" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSURL *fontURL = [bundle URLForResource:fontName withExtension:@"ttf"];
NSData *fontData = [NSData dataWithContentsOfURL:fontURL];
Expand All @@ -25,16 +30,20 @@ void KOSLoadFontWithName(NSString *fontName) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:(__bridge NSString *)errorDescription userInfo:@{ NSUnderlyingErrorKey: (__bridge NSError *)error }];
}

CFRelease(font);
}

CFRelease(provider);
}

@end

@implementation UIFont (OpenSans)

+ (instancetype)kosLoadAndReturnFont:(NSString *)fontName size:(CGFloat)fontSize onceToken:(dispatch_once_t *)onceToken fontFileName:(NSString *)fontFileName {
dispatch_once(onceToken, ^{
KOSLoadFontWithName(fontFileName);
[KOSFontLoader loadFontWithName:fontFileName];
});

return [self fontWithName:fontName size:fontSize];
Expand Down Expand Up @@ -91,4 +100,3 @@ + (instancetype)openSansExtraBoldItalicFontOfSize:(CGFloat)size {
}

@end

0 comments on commit 874e65b

Please sign in to comment.