访问UIKit或CoreText中的OpenType功能 [英] Accessing OpenType Features in UIKit or CoreText

查看:94
本文介绍了访问UIKit或CoreText中的OpenType功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenType(.otf)格式的自定义字体,并希望使用字体的OpenType功能

I am using a custom font in OpenType(.otf) format and would like to use some of the OpenType Features of the font.

如何使用UIKit或CoreText实现此目的?我显然更喜欢UIKit,但是看看 UIFont ,选项非常有限。

How can I accomplish this with UIKit or CoreText? I would obviously prefer UIKit, but looking at UIFont, the options are extremely limited.

除了可以使用字体格式外,似乎完全没有关于iOS上OpenType支持的文档。

There seems to be a complete absence of documentation regarding OpenType support on iOS, except that the font format can be used.

相关阅读:微软的参考资料OpenType功能,以及浏览器开始提供OpenType功能支持的一些信息。虽然这个问题是在iOS上本地渲染具有OpenType功能的字体。

Related reading: Microsoft's reference for OpenType features, and some info on how browsers are beginning to offer OpenType feature support. Though this question is for rendering fonts with OpenType features on iOS natively.

推荐答案

因为我所听到的只是在这黑暗中的蟋蟀和寂寞的Core Text地方,我想我会发布我自己找到的答案。

Since all I'm hearing are crickets in this dark and lonesome Core Text place, I thought I'd post the answer I found on my own.

答案是UIKit不支持在其框架内设置OpenType功能(在撰写本文时),您必须下载到Core Text才能执行此操作,幸运的是,他们确实拥有一个公开其他字体功能的API。

The answer is UIKit does not support the setting of OpenType features within its framework(at the time of writing), you have to drop down to Core Text to do it, and luckily they do have an API that exposes additional font features.

我赢了我们将详细介绍如何使用Core Text绘制文本,但相关的想法是你需要得到一个 CTFontDescriptorRef ,它定义了将要使用的字体的所有属性绘制文字。

I won't go into detail about using Core Text to draw text, but the relevant idea is you will need to get a CTFontDescriptorRef that defines all the attributes of the font that will be used to draw your text.

示例代码:

CTFontDescriptorRef fontDescriptorNoFeatures = CTFontDescriptorCreateWithNameAndSize((__bridge CFStringRef)self.font.fontName, pointSize);

// Set up OpenType Attributes
CFAllocatorRef defaultAllocator = CFAllocatorGetDefault();

int numberSpacing = kNumberSpacingType;
int numberSpacingType = kMonospacedNumbersSelector;

CFNumberRef numberSpacingId = CFNumberCreate(defaultAllocator, kCFNumberIntType, &numberSpacing);
CFNumberRef monospacedNumbersSelector = CFNumberCreate(defaultAllocator, kCFNumberIntType, &numberSpacingType);

CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateCopyWithFeature(fontDescriptorNoFeatures, numberSpacingId, monospacedNumbersSelector);

CFRelease(fontDescriptorNoFeatures);
CFRelease(numberSpacingId);
CFRelease(monospacedNumbersSelector);

我在这里做的主要是制作副本,使用 CTFontDescriptorCreateCopyWithFeature() ,具有附加功能的普通字体描述符,在OpenType中称为表格数字,但在核心文本中,您可以使用数字间距功能访问此功能( kNumberSpacingType ),并设置 < CoreText / SFNTLayoutTypes.h> 中定义的相应枚举的值

The main thing I am doing here is making a copy, using CTFontDescriptorCreateCopyWithFeature(), of the normal font descriptor with an additional feature, in OpenType it is called "Tabular Figures", but in Core Text you would access this feature by using the number spacing feature (kNumberSpacingType), and set the value for the appropriate enum defined in <CoreText/SFNTLayoutTypes.h>.

对于数字间距功能,枚举值(由于某种原因,他们称之为选择器!?!)是:

For the number spacing feature the enum values(for some reason they call them selectors!?!) are:

enum {
  kMonospacedNumbersSelector    = 0,
  kProportionalNumbersSelector  = 1,
  kThirdWidthNumbersSelector    = 2,
  kQuarterWidthNumbersSelector  = 3
};

所以诀窍是没有OpenType到CoreText功能的直接一对一映射,但看起来它们都在那里,你只需要通过查看 < CoreText / SFNTLayoutTypes.h>

So the trick is there isn't a direct one-to-one mapping of OpenType to CoreText features, but it appears they all are there, you'll just need to go through the pain of identifying the feature by looking through the constants defined in <CoreText/SFNTLayoutTypes.h>.

现在你必须在Core Text中使用这种字体绘制文本,而不是更高级别的视图,但有很多参考资料。

The rest of the pain is now you have to draw the text with this font in Core Text, instead of a higher level view, but there are lots of references out there for doing that.

这篇关于访问UIKit或CoreText中的OpenType功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆