forked from CocoaPods-Fonts/Montserrat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIFont+Montserrat.m
62 lines (45 loc) · 2.01 KB
/
UIFont+Montserrat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// UIFont+Montserrat.m
//
// Created by Kyle Fuller on 08/08/2014
//
#import <CoreText/CoreText.h>
#import "UIFont+Montserrat.h"
@interface KOSFontLoader : NSObject
+ (void)loadFontWithName:(NSString *)fontName;
@end
@implementation KOSFontLoader
+(void) loadFontWithName:(NSString *)fontName {
NSURL *bundleURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"Montserrat" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSURL *fontURL = [bundle URLForResource:fontName withExtension:@"ttf"];
NSData *fontData = [NSData dataWithContentsOfURL:fontURL];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (font) {
CFErrorRef error = NULL;
if (CTFontManagerRegisterGraphicsFont(font, &error) == NO) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:(__bridge NSString *)errorDescription userInfo:@{ NSUnderlyingErrorKey: (__bridge NSError *)error }];
}
CFRelease(font);
}
CFRelease(provider);
}
@end
@implementation UIFont (Montserrat)
+ (instancetype)montserratLoadAndReturnFont:(NSString *)fontName size:(CGFloat)fontSize onceToken:(dispatch_once_t *)onceToken fontFileName:(NSString *)fontFileName {
dispatch_once(onceToken, ^{
[KOSFontLoader loadFontWithName:fontFileName];
});
return [self fontWithName:fontName size:fontSize];
}
+ (instancetype)montserratFontOfSize:(CGFloat)fontSize {
static dispatch_once_t onceToken;
return [self montserratLoadAndReturnFont:@"Montserrat" size:fontSize onceToken:&onceToken fontFileName:@"Montserrat-Regular"];
}
+ (instancetype)montserratBoldFontOfSize:(CGFloat)fontSize {
static dispatch_once_t onceToken;
return [self montserratLoadAndReturnFont:@"Montserrat-Bold" size:fontSize onceToken:&onceToken fontFileName:@"Montserrat-Bold"];
}
@end