简体   繁体   English

Cocoa应用程序中的自定义字体

[英]Custom font in a Cocoa application

I know you can customize fonts by using Interface Builder and selecting a font. 我知道您可以使用Interface Builder自定义字体并选择字体。 However, I'm curious if I can use a custom font that's not included by default on systems. 但是,我很好奇我是否可以使用系统上默认不包含的自定义字体。 Is there a way to include a custom font in my application? 有没有办法在我的应用程序中包含自定义字体?

While the manual font activation procedure is one option, you might also consider the ATSApplicationFontsPath Info.plist key: 虽然手动字体激活过程是一个选项,您也可以考虑ATSApplicationFontsPath Info.plist键:

Information Property List Key Reference : 信息财产清单主要参考

" ATSApplicationFontsPath ( String - Mac OS X) identifies the location of a font file or directory of fonts in the bundle's Resources directory. If present, Mac OS X activates the fonts at the specified path for use by the bundled application. The fonts are activated only for the bundled application and not for the system as a whole. The path itself should be specified as a relative directory of the bundle's Resources directory. For example, if a directory of fonts was at the path /Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/ , you should specify the string Stuff/MyFonts/ for the value of this key." ATSApplicationFontsPathString - Mac OS X)标识StringResources目录中字体文件或字体目录的位置。如果存在,Mac OS X将激活指定路径上的字体以供捆绑的应用程序使用。字体被激活只有捆绑的应用程序而不是整个系统。路径本身应该被指定为bundle的Resources目录的相对目录。例如,如果一个字体目录位于路径/Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/ ,你应该为这个键的值指定字符串Stuff/MyFonts/ 。“

I'd be sure to double-check, but I believe this functionality was added in OS X 10.5.x (which the code posted by Jinhyung Park targets). 我一定要仔细检查,但我相信这个功能是在OS X 10.5.x中添加的(由Jinhyung Park发布的代码目标)。

ATSApplicationFontsPath uses [NSBundle mainbundle] path as base path, so it does not work when Resources folder is not located there (eg for app plugins). ATSApplicationFontsPath使用[NSBundle mainbundle]路径作为基本路径,因此当Resources文件夹不在那里时它不起作用(例如对于app插件)。

In my Cocoa plugin I need to load custom fonts using CTFontManagerRegisterFontsForURL 在我的Cocoa插件中,我需要使用CTFontManagerRegisterFontsForURL加载自定义字体

#import <Cocoa/Cocoa.h>

static void FGDActivateFont(NSString *fontName)
{

    // Can't make ATSApplicationFontsPath in Info.plist work for plugins (non-standard Resources path)

    NSArray *availableFonts = [[NSFontManager sharedFontManager] availableFonts];

    if (![availableFonts containsObject:fontName]) {

        NSURL *fontURL = [[FGDRapidCart bundle] URLForResource:fontName withExtension:@"ttf" subdirectory:@"Fonts"];
        assert(fontURL);
        CFErrorRef error = NULL;
        if (!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontURL, kCTFontManagerScopeProcess, &error))
        {
            CFShow(error);
        }

    }

}

int main(int argc, char *argv[])
{
    @autoreleasepool
    {
        FGDActivateFont(@“FontAwesome”);
    }
    return NSApplicationMain(argc, (const char **)argv);
}

Credits: https://github.com/OneSadCookie/Extendaword/blob/master/Extendaword/main.m 致谢: https//github.com/OneSadCookie/Extendaword/blob/master/Extendaword/main.m

Here is the example for Mac App custom font loading. 以下是Mac App自定义字体加载的示例。

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;
    }
}

I have managed to do this using cocos2d (CCLabelBMFont) and hiero tool. 我已经设法使用cocos2d(CCLabelBMFont)和hiero工具完成了这项工作。 You will need to create the font using the hiero, and give this font to the CCLabelBMFont object. 您需要使用hiero创建字体,并将此字体提供给CCLabelBMFont对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM