Load a NSFont from bundle.
First, add ApplicationServices.framework to project.
First, add ApplicationServices.framework to project.
- + (CTFontRef) fontFromBundle : (NSString*) fontName withHeight : (CGFloat) height;
- {
- // Get the path to our custom font and create a data provider.
- NSString* fontPath = [[NSBundle mainBundle] pathForResource : fontName
- ofType : @"ttf" ];
- if (nil==fontPath)
- return NULL;
- CGDataProviderRef dataProvider =
- CGDataProviderCreateWithFilename ([fontPath UTF8String]);
- if (NULL==dataProvider)
- return NULL;
- // Create the font with the data provider, then release the data provider.
- CGFontRef fontRef = CGFontCreateWithDataProvider ( dataProvider );
- if ( NULL == fontRef )
- {
- CGDataProviderRelease ( dataProvider );
- return NULL;
- }
- CTFontRef fontCore = CTFontCreateWithGraphicsFont(fontRef, height, NULL, NULL);
- CGDataProviderRelease (dataProvider);
- CGFontRelease(fontRef);
- return fontCore;
- }
In code
- CTFontRef bundleFont = [ CustomFonts fontFromBundle : @"MavenPro-Regular"
- withHeight : 25 ];
- NSFont* font = (NSFont*)bundleFont;
- ....
- // use NSFont
- ....
- CFRelease(bundleFont);
UPD. works correctly only 10.7+