For licensing reasons, I have to decrypt the font file and load it into memory, and then register it instead of reading directly from the URL. For this I have to use CTFontManagerRegisterGraphicsFont .
The problem part occurs when I try to use [NSFont fontWithName:@"Open Sans" size:21.0] , where it will only accept the name of the PostScript font (ie ("OpenSans or OpenSans-Bold for Bold Weight") and will not work with the surname "Open Sans".
If the font is registered using ATSApplicationFontsPath in the ATSApplicationFontsPath file or using CTFontManagerRegisterFontsForURLs , then I can use the font name. But I canβt do it.
("Open Sans" is used here as an example)
Here is the relevant code that I use to register the font.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"OpenSans-Regular" ofType:@"ttf" inDirectory:@"Fonts"]; NSData *fontData = [NSData dataWithContentsOfFile:fontPath]; CGDataProviderRef fontProviderRef = CGDataProviderCreateWithCFData((CFDataRef)fontData); CGFontRef fontRef = CGFontCreateWithDataProvider(fontProviderRef); CFErrorRef error; if (! CTFontManagerRegisterGraphicsFont(fontRef, &error)) { CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(fontRef); CFRelease(fontProviderRef);
Is there a way to make the font family name available to NSFont using CTFontManagerRegisterGraphicsFont ?
(Using Xcode 5 and targeting on Mac OS X> = 10.8)
objective-c cocoa macos
Salaryman
source share