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

标签 iphone ios xcode static-libraries fontconfig

我有以下问题:

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

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

Fontconfig error: Cannot load default config file

terminate called throwing an exception

我读到这个错误通常发生在 FontConfig 找不到通常位于/etc/fonts 中的 fonts.conf 文件时。

但是我无法将该文件移动到 iPhone 的这个目录中。有人有解决办法吗?仅将文件复制到应用程序包也无济于事。

如有任何帮助,我将不胜感激。

最佳答案

我终于找到了解决这个问题的方法。我设置了一个环境变量 FONTCONFIG_FILE 和 FONTCONFIG_PATH,它们定义了 fonts.conf 文件的位置以及 /etc/fonts 之前的路径。所以我可以将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); 

在第二步中,我必须修改我的 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>

到:

<!-- Font directory list -->

<dir>FC/fonts</dir>

第二部分:

<!-- Font cache directory list -->

<cachedir>/opt/local/fc1407cd/var/cache/fontconfig</cachedir>
<cachedir>~/.fontconfig</cachedir>

到:

<!-- Font cache directory list -->

<cachedir>FC/cache</cachedir>

然后它就不再崩溃了。

关于iPhone:字体配置错误:无法加载默认配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471538/

相关文章:

php - 如何用 PHP 解密 iPhone 加密的 AES256 文本

iphone - 如何在操作中找到正确的 UIButton 对象

swift - 获取用户位置在 Xcode 中不起作用

xcode - 执行从 Xib 到 ViewController 的转场

iphone - 通过代码将 PDF 添加到 iBooks

ios - 检查 UIScrollView 是否到达顶部或底部

iphone - CoreLocation 精度问题

ios - RestKit 中每个请求的自定义 header

ios - 在 iOS Swift 中设置按钮事件颜色的简单方法

ios - SwiftHTTP 已安装但无法导入