iPhone:Fontconfig错误:无法加载默认配置文件

el *_*lex 5 iphone xcode static-libraries fontconfig ios

我有以下问题:

我在我的iPhone项目中包含了FontConfig libfontconfig.a的静态库.

应用程序正在构建,我可以在设备上运行它但是当我调用需要此库的某些功能的方法时,应用程序崩溃并出现以下错误

Fontconfig错误:无法加载默认配置文件

终止调用抛出异常

我读到,当FontConfig找不到通常位于/ etc/fonts中的fonts.conf文件时,通常会发生此错误.

但是我无法将该文件移动到iPhone的这个目录中.有人有解决方案吗?只是将文件复制到应用程序包也没有帮助.

我将不胜感激任何帮助.

el *_*lex 4

我终于找到了解决这个问题的方法。我设置了环境变量FONTCONFIG_FILE 和 FONTCONFIG_PATH 来定义 fonts.conf 文件的位置以及/etc/fonts之前的路径。所以我可以将字体目录添加到项目中并定义路径@runtime。

在调用需要 FontConfig 功能的方法之前,必须在代码中设置环境变量。我在 AppDelegate 中启动应用程序后设置它。

这是我添加的代码:

//Get the directory of your Application
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];

//Create a String with the location of your fonts.conf file
NSString *fontconfigFile= [bundlePath stringByAppendingString:
                             [NSString stringWithFormat:@"/fonts/fonts.conf"]];

//Create a String with the path of the FontConfig configuration path
NSString *fontconfigPath= [bundlePath stringByAppendingString:
                             [NSString stringWithFormat:@"/fonts"]];

//Set the Environment Variables
setenv("FONTCONFIG_FILE", [fontconfigFile UTF8String], 0); 
setenv("FONTCONFIG_PATH", [fontconfigPath UTF8String], 0); 
Run Code Online (Sandbox Code Playgroud)

在第二步中,我必须修改我的 fonts.conf 文件。我将字体目录添加到项目的FC/fonts中,将缓存目录添加到项目的FC/cache中,并调整了两部分的 fonts.conf 文件。

我改变了第一部分:

<!-- Font directory list -->

<dir>/usr/share/fonts</dir>
<dir>/usr/X11R6/lib/X11/fonts</dir>
<dir>~/.fonts</dir>
Run Code Online (Sandbox Code Playgroud)

到:

<!-- Font directory list -->

<dir>FC/fonts</dir>
Run Code Online (Sandbox Code Playgroud)

和第二部分:

<!-- Font cache directory list -->

<cachedir>/opt/local/fc1407cd/var/cache/fontconfig</cachedir>
<cachedir>~/.fontconfig</cachedir>
Run Code Online (Sandbox Code Playgroud)

到:

<!-- Font cache directory list -->

<cachedir>FC/cache</cachedir>
Run Code Online (Sandbox Code Playgroud)

然后就没有再崩溃了。