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

发布于 2024-10-21 07:24:33 字数 99 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你穿错了嫁妆 2024-10-28 07:24:33

虽然手动字体激活过程是一种选择,但您也可以考虑 ATSApplicationFontsPath Info.plist 键:

信息属性列表密钥参考

ATSApplicationFontsPath字符串 - Mac
OS X) 识别某个位置
字体文件或字体目录
捆绑包的 Resources 目录。如果
目前,Mac OS X 激活字体
在指定的路径供使用
捆绑的应用程序。字体是
仅针对捆绑激活
应用程序而不是系统
一个整体。路径本身应该是
指定为相对目录
捆绑包的资源目录。为了
例如,如果字体目录是
在路径上
/Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/,
你应该指定字符串
Stuff/MyFonts/ 这个值
关键。”

我一定要仔细检查,但我相信这个功能是在 OS X 10.5.x 中添加的(Jinhyung Park 发布的代码所针对的)。

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

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."

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).

陌上芳菲 2024-10-28 07:24:33

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

在我的 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);
}

积分: https:/ /github.com/OneSadCookie/Extendaword/blob/master/Extendaword/main.m

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).

In my Cocoa plugin I need to load custom fonts using 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

韶华倾负 2024-10-28 07:24:33

以下是 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;
    }
}

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;
    }
}
江挽川 2024-10-28 07:24:33

我已经设法使用 cocos2d (CCLabelBMFont) 和 hiero 工具来做到这一点。您需要使用 hiero 创建字体,并将该字体提供给 CCLabelBMFont 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文