-
Notifications
You must be signed in to change notification settings - Fork 2
/
UIFont+Inconsolata.m
53 lines (39 loc) · 1.86 KB
/
UIFont+Inconsolata.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
//
// UIFont+Inconsolata.m
//
//
#import <CoreText/CoreText.h>
#import "UIFont+Inconsolata.h"
@implementation UIFont (Inconsolata)
void InconsolataLoadFontWithName(NSString *fontName) {
NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"Inconsolata" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSURL *fontURL = [bundle URLForResource:fontName withExtension:nil];
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);
}
+ (instancetype)inconsolataLoadAndReturnFont:(NSString *)fontName size:(CGFloat)fontSize onceToken:(dispatch_once_t *)onceToken fontFileName:(NSString *)fontFileName {
dispatch_once(onceToken, ^{
InconsolataLoadFontWithName(fontFileName);
});
return [self fontWithName:fontName size:fontSize];
}
+ (instancetype)inconsolataFontOfSize:(CGFloat)fontSize {
static dispatch_once_t onceToken;
return [self inconsolataLoadAndReturnFont:@"Inconsolata-Regular" size:fontSize onceToken:&onceToken fontFileName:@"Inconsolata-Regular.ttf"];
}
+ (instancetype)inconsolataBoldFontOfSize:(CGFloat)fontSize {
static dispatch_once_t onceToken;
return [self inconsolataLoadAndReturnFont:@"Inconsolata-Bold" size:fontSize onceToken:&onceToken fontFileName:@"Inconsolata-Bold.ttf"];
}
@end