mvm_core_ui/MVMCoreUI/Utility/MFFonts.m
Xinlei(Ryan) Pan 4ad4eb7f46 add new font
2020-02-25 11:49:26 -05:00

82 lines
2.4 KiB
Objective-C

//
// MFFonts.m
// myverizon
//
// Created by Scott Pfeil on 11/17/14.
// Copyright (c) 2014 Verizon Wireless. All rights reserved.
//
#import "MFFonts.h"
#import <CoreText/CoreText.h>
#import "MVMCoreUIUtility.h"
@import MVMCore.MVMCoreLoggingHandler;
static NSString * const DSBold = @"VerizonNHGeDS-Bold";
static NSString * const DSRegular = @"VerizonNHGeDS-Regular";
static NSString * const TXBold = @"VerizonNHGeTX-Bold";
static NSString * const TXRegular = @"VerizonNHGeTX-Regular";
@implementation MFFonts
+ (void)loadMVMFonts {
static dispatch_once_t once;
dispatch_once(&once, ^{
[MFFonts loadFont:DSBold type:@"otf"];
[MFFonts loadFont:DSRegular type:@"otf"];
[MFFonts loadFont:TXBold type:@"otf"];
[MFFonts loadFont:TXRegular type:@"otf"];
[MFFonts loadFont:@"OCRAExtended" type:@"ttf"];
});
}
+ (void)loadFont:(NSString *)fontName type:(NSString *)type{
NSString *fontPath = [[MVMCoreUIUtility bundleForMVMCoreUI] pathForResource:fontName ofType:type];
NSData *inData = [NSData dataWithContentsOfFile:fontPath];
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
[UIFont familyNames];//fixing weird app freeze after launch app issue
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
MVMCoreLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
}
+ (UIFont *)mfFont75Bd:(CGFloat)size {
[self loadMVMFonts];
UIFont *font;
if (size >= 15) {
font = [UIFont fontWithName:DSBold size:size];
} else {
font = [UIFont fontWithName:TXBold size:size];
}
return font ?: [UIFont boldSystemFontOfSize:size];
}
+ (UIFont *)mfFont55Rg:(CGFloat)size {
[self loadMVMFonts];
UIFont *font;
if (size >= 15) {
font = [UIFont fontWithName:DSRegular size:size];
} else {
font = [UIFont fontWithName:TXRegular size:size];
}
return font ?: [UIFont systemFontOfSize:size];
}
+ (nullable UIFont *)mfFontOcratxt:(CGFloat)size {
[self loadMVMFonts];
return [UIFont fontWithName:@"OCRAExtended" size:size];
}
+ (UIFont *)mfFontWithName:(nonnull NSString *)name size:(CGFloat)size {
return [UIFont fontWithName:name size:size] ?: [self mfFont55Rg:size];
}
@end