Custom font in a Cocoa application

前端 未结 4 1115
渐次进展
渐次进展 2020-12-08 01:23

I know you can customize fonts by using Interface Builder and selecting a font. However, I\'m curious if I can use a custom font that\'s not included by default on systems.

4条回答
  •  悲哀的现实
    2020-12-08 01:39

    Here is the example for Mac App custom font loading.

    NSString *fontFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/fonts"];
    fontsURL = [NSURL fileURLWithPath:fontFilePath];
    if(fontsURL != nil)
    {
        OSStatus status;
        FSRef fsRef;
        CFURLGetFSRef((CFURLRef)fontsURL, &fsRef);
        status = ATSFontActivateFromFileReference(&fsRef, kATSFontContextLocal, kATSFontFormatUnspecified, 
                                                  NULL, kATSOptionFlagsDefault, NULL);
        if (status != noErr)
        {
            errorMessage = @"Failed to acivate fonts!";
            goto error;
        }
    }
    

提交回复
热议问题