Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Precise the constant MyFont to MyFontFileNameWithoutExtension due to a good comment about this
Source Link
Nicolas Lauquin
  • 1.5k
  • 3
  • 14
  • 24

Here is way I implemented it for my fmk based on the solution provided by "David M." This solution doesn't require to add the reference to the font in the plist.

  1. Class that load the font

    • (void) loadMyCustomFont{ NSString *fontPath = [[NSBundle frameworkBundle] pathForResource:@"MyFont"@"MyFontFileNameWithoutExtension" ofType:@"ttf"]; NSData *inData = [NSData dataWithContentsOfFile:fontPath]; CFErrorRef error; CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData); CGFontRef font = CGFontCreateWithDataProvider(provider); if (! CTFontManagerRegisterGraphicsFont(font, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(font); CFRelease(provider); }
  2. Category on NSBundle to get access to my bundle

    • (NSBundle )frameworkBundle { static NSBundle frameworkBundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyBundleName.bundle"]; frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; }); return frameworkBundle; }

Note: require to integrate CoreText in your project

Here is way I implemented it for my fmk based on the solution provided by "David M." This solution doesn't require to add the reference to the font in the plist.

  1. Class that load the font

    • (void) loadMyCustomFont{ NSString *fontPath = [[NSBundle frameworkBundle] pathForResource:@"MyFont" ofType:@"ttf"]; NSData *inData = [NSData dataWithContentsOfFile:fontPath]; CFErrorRef error; CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData); CGFontRef font = CGFontCreateWithDataProvider(provider); if (! CTFontManagerRegisterGraphicsFont(font, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(font); CFRelease(provider); }
  2. Category on NSBundle to get access to my bundle

    • (NSBundle )frameworkBundle { static NSBundle frameworkBundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyBundleName.bundle"]; frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; }); return frameworkBundle; }

Note: require to integrate CoreText in your project

Here is way I implemented it for my fmk based on the solution provided by "David M." This solution doesn't require to add the reference to the font in the plist.

  1. Class that load the font

    • (void) loadMyCustomFont{ NSString *fontPath = [[NSBundle frameworkBundle] pathForResource:@"MyFontFileNameWithoutExtension" ofType:@"ttf"]; NSData *inData = [NSData dataWithContentsOfFile:fontPath]; CFErrorRef error; CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData); CGFontRef font = CGFontCreateWithDataProvider(provider); if (! CTFontManagerRegisterGraphicsFont(font, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(font); CFRelease(provider); }
  2. Category on NSBundle to get access to my bundle

    • (NSBundle )frameworkBundle { static NSBundle frameworkBundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyBundleName.bundle"]; frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; }); return frameworkBundle; }

Note: require to integrate CoreText in your project

Source Link
Nicolas Lauquin
  • 1.5k
  • 3
  • 14
  • 24

Here is way I implemented it for my fmk based on the solution provided by "David M." This solution doesn't require to add the reference to the font in the plist.

  1. Class that load the font

    • (void) loadMyCustomFont{ NSString *fontPath = [[NSBundle frameworkBundle] pathForResource:@"MyFont" ofType:@"ttf"]; NSData *inData = [NSData dataWithContentsOfFile:fontPath]; CFErrorRef error; CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData); CGFontRef font = CGFontCreateWithDataProvider(provider); if (! CTFontManagerRegisterGraphicsFont(font, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(font); CFRelease(provider); }
  2. Category on NSBundle to get access to my bundle

    • (NSBundle )frameworkBundle { static NSBundle frameworkBundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath]; NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"MyBundleName.bundle"]; frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath]; }); return frameworkBundle; }

Note: require to integrate CoreText in your project