-
Notifications
You must be signed in to change notification settings - Fork 17
/
UIFont+OpenSans.m
102 lines (77 loc) · 3.78 KB
/
UIFont+OpenSans.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// UIFont+OpenSans.m
//
// Created by Kyle Fuller on 18/02/2014
//
#import <CoreText/CoreText.h>
#import <OpenSans/UIFont+OpenSans.h>
@interface KOSFontLoader : NSObject
+ (void)loadFontWithName:(NSString *)fontName;
@end
@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];
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 (OpenSans)
+ (instancetype)kosLoadAndReturnFont:(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)openSansFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"Open Sans" size:size onceToken:&onceToken fontFileName:@"OpenSans-Regular"];
}
+ (instancetype)openSansItalicFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-Italic" size:size onceToken:&onceToken fontFileName:@"OpenSans-Italic"];
}
+ (instancetype)openSansLightFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-Light" size:size onceToken:&onceToken fontFileName:@"OpenSans-Light"];
}
+ (instancetype)openSansLightItalicFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSansLight-Italic" size:size onceToken:&onceToken fontFileName:@"OpenSans-LightItalic"];
}
+ (instancetype)openSansBoldFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-Bold" size:size onceToken:&onceToken fontFileName:@"OpenSans-Bold"];
}
+ (instancetype)openSansBoldItalicFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-BoldItalic" size:size onceToken:&onceToken fontFileName:@"OpenSans-BoldItalic"];
}
+ (instancetype)openSansSemiBoldFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-Semibold" size:size onceToken:&onceToken fontFileName:@"OpenSans-Semibold"];
}
+ (instancetype)openSansSemiBoldItalicFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-SemiboldItalic" size:size onceToken:&onceToken fontFileName:@"OpenSans-SemiboldItalic"];
}
+ (instancetype)openSansExtraBoldFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-Extrabold" size:size onceToken:&onceToken fontFileName:@"OpenSans-ExtraBold"];
}
+ (instancetype)openSansExtraBoldItalicFontOfSize:(CGFloat)size {
static dispatch_once_t onceToken;
return [self kosLoadAndReturnFont:@"OpenSans-ExtraboldItalic" size:size onceToken:&onceToken fontFileName:@"OpenSans-ExtraBoldItalic"];
}
@end