Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocoa应用程序中的自定义字体_Objective C_Cocoa_Macos - Fatal编程技术网

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

Objective c Cocoa应用程序中的自定义字体,objective-c,cocoa,macos,Objective C,Cocoa,Macos,我知道您可以通过使用Interface Builder和选择字体来自定义字体。但是,我很好奇是否可以使用默认情况下系统中未包含的自定义字体。有没有办法在我的应用程序中包含自定义字体?我已经使用cocos2d(CCLabelBMFont)和hiero工具实现了这一点。您需要使用hiero创建字体,并将此字体提供给CCLabelBMFont对象。以下是Mac App自定义字体加载的示例 NSString *fontFilePath = [[[NSBundle mainBundle] resource

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

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

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

虽然手动字体激活过程是一种选择,但您也可以考虑<代码> ATSApplicationFontsPath <代码>信息。PLIST键:

:

ATSApplicationFontsPath
String
-Mac OS X)标识一个 中的字体文件或字体目录 捆绑包的
资源
目录。如果 目前,Mac OS X激活字体 在指定的路径上,以供 捆绑应用程序。字体为 仅为捆绑的应用程序激活 应用程序,而不是系统作为 一个整体。路径本身应该是 指定为的相对目录 捆绑包的资源目录 例如,如果创建了字体目录 在小路上
/Applications/MyApp.app/Contents/Resources/Stuff/MyFonts/
, 您应该指定字符串
Stuff/MyFonts/
获取此文件的值 钥匙。”


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

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

在我的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);
}
#导入
静态无效FGDActivateFont(NSString*fontName)
{
//无法使Info.plist中的ATSApplicationFontsPath对插件起作用(非标准资源路径)
NSArray*availableFonts=[[NSFontManager sharedFontManager]availableFonts];
如果(![availableFonts containsObject:fontName]){
NSURL*fontURL=[[FGDRapidCart bundle]URLForResource:fontName,扩展名:@“ttf”子目录:@“Fonts”];
断言(fontURL);
CFErrorRef error=NULL;
if(!CTFontManagerRegisterFontsForURL((_桥CFURLRef)fontURL、kCTFontManagerScopeProcess和错误))
{
CFShow(错误);
}
}
}
int main(int argc,char*argv[])
{
@自动释放池
{
FGDActivateFont(@“FontAwesome”);
}
返回NSApplicationMain(argc,(const char**)argv);
}

信用证:

@poke:这不是复制品,这是给Mac的,在我的标签中有明确的说明。请仔细阅读。@poke:没关系。:)我猜这可能是更好的复制品:–现在不能改变我的投票结果:/@poke:看起来很完美。虽然我找过了,但我没有看到它。:)另一方面,我尝试在应用程序中嵌入Calibri是否合法?这不太可能,因为它是MS字体。你应该检查一下许可协议。我的问题是关于Mac,而不是关于iPhone。你能不能打电话给
[NSFont fontWithName:@“fontname”]
?我在上读了讨论。加载字体后,您可以使用[NSFont fontWithName:@”“]。我刚刚再次检查了我的Mac OS X应用程序是否正常工作。有趣的是,在从文件引用中搜索ATSFontactivate时,在Xcode帮助或ADC中都找不到任何内容-需要转到硬盘上的“ATS/ATSFont.h”以获取任何信息。..ATSFontActivateFromFileReference=Undocumented=App StoreRejection@Tibidabo几年后,但一点也不非法:是的,或者你可以使用雪豹中引入的核心文本,我相信。这个解决方案是正确的。谢谢你指出,这样容易多了。谢谢,我不知道为什么我以前没有把这个标记为答案,但现在你知道了。:)你能帮我解决同样的问题吗,回答得好,我的屏幕保护程序需要这个,plist值在那里不起作用。