可可 – 如何使用Core Text从文件加载字体(TTF)?

在OSX 10.6之前,ATSFontActivateFromFileSpecification / ATSFontActivateFromFileReference可用,可用于从文件加载字体.在Core Text中找不到任何类似的东西.

解决方法

您可以通过CGFontRef从字体文件获取CTFontRef:
CFURLRef url = CFURLCreateWithFileSystemPath(kcfAllocatorDefault,CFSTR("/path/to/font"),kcfURLPOSIXPathStyle,false);
CGDataProviderRef dataProvider = CGDataProviderCreateWithURL(url);
CGFontRef theCGFont = CGFontCreateWithDataProvider(dataProvider);
CTFontRef theCTFont = CTFontCreateWithGraphicsFont(theCGFont);
CFRelease(theCGFont);
CFRelease(dataProvider);
CFRelease(url);

// do something with the CTFontRef here

CFRelease(theCTFont);

相关文章

Mip是什么意思以及作用有哪些
怎么测试Mip页面运行情况
MIP安装的具体步骤有哪些
HTML添加超链接、锚点的方法及作用详解(附视频)
MIP的规则有哪些
Mip轮播图组件中的重要属性讲解