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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5283572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:49:22  来源:igfitidea点击:

Custom font in a Cocoa application

objective-ccocoamacos

提问by sudo rm -rf

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. Is there a way to include a custom font in my application?

我知道您可以通过使用 Interface Builder 并选择字体来自定义字体。但是,我很好奇是否可以使用系统默认不包含的自定义字体。有没有办法在我的应用程序中包含自定义字体?

回答by NSGod

While the manual font activation procedure is one option, you might also consider the ATSApplicationFontsPathInfo.plist key:

虽然手动字体激活过程是一种选择,但您也可以考虑使用ATSApplicationFontsPathInfo.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 Resourcesdirectory. 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."

" ATSApplicationFontsPath( String- Mac OS X) 标识字体文件或字体目录在捆绑Resources目录中的位置。如果存在,Mac OS X 会在指定路径激活字体以供捆绑应用程序使用。字体仅针对捆绑的应用程序,而不是整个系统。路径本身应指定为捆绑资源目录的相对目录。例如,如果字体目录位于路径中 /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 发布的代码针对的)中添加的。

回答by mzf

ATSApplicationFontsPath uses [NSBundle mainbundle] path as base path, so it does not work when Resources folder is not located there (e.g. for app plugins).

ATSApplicationFontsPath 使用 [NSBundle mainbundle] 路径作为基本路径,因此当资源文件夹不在那里时它不起作用(例如对于应用程序插件)。

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

回答by Jinhyung Park

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

回答by Basel

I have managed to do this using cocos2d (CCLabelBMFont) and hiero tool. You will need to create the font using the hiero, and give this font to the CCLabelBMFont object.

我已经使用 cocos2d (CCLabelBMFont) 和 hiero 工具成功地做到了这一点。您将需要使用 hiero 创建字体,并将此字体提供给 CCLabelBMFont 对象。