Access OpenType features in UIKit or CoreText

I use my own font in the OpenType format (.otf) and would like to use some of the features of the OpenType font.

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

It seems that there is a complete lack of documentation regarding OpenType support in iOS, except that you can use the font format.

Related reading: Microsoft link for OpenType features , as well as some information on how browsers are starting to offer OpenType support . Although this question is intended for rendering fonts with OpenType features on iOS natively.

+7
ios uikit core-text
source share
1 answer

Since all I hear is crickets in this dark and lonely place with Core Text, I thought I would post an answer that I found myself.

Answer: UIKit does not support customizing OpenType functions in its structure (at the time of writing), you need to go to Core Text to do this, and, fortunately, they have an API that provides additional font functions.

I will not go into details about using Core Text to draw text, but the corresponding idea is that you need to get a CTFontDescriptorRef that defines all the font attributes that will be used to draw your text.

Code example:

 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); 

The main thing I do here is to make a copy using CTFontDescriptorCreateCopyWithFeature() , a normal font descriptor with an additional function, called "Table Shapes" in OpenType, but in Core Text you must access this function using the number spacing function ( kNumberSpacingType ) and set the value for the corresponding enumeration defined in <CoreText/SFNTLayoutTypes.h> ,

For a number with an interval, the enumeration values ​​(for some reason they call them selectors!?!):

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

So, the trick is that there is no direct one-to-one mapping of OpenType functions with CoreText, but it seems that they are all there, you just need to go through the pain to identify the function by looking at the constants defined in <CoreText/SFNTLayoutTypes.h> .

The rest of the pain now you need to draw the text using this font in Core Text, and not at a higher level, but there are many links for this.

+7
source share

All Articles