Strengthen UIFont for rendering as monospaced

Is it possible to make a UIFont be monospace?

In particular, I use a font that does not contain monospace numbers (table digits). Creating a modified font object that is monospaced and adding that font to the numerical segments of attribute strings will work just fine.

Another solution might be to add custom attributes to the attributed string and change what the layout of the text processes to use a fixed size for glyphs in certain ranges.

Things that didn't work:

There are a number of questions that offer solutions to such problems by overriding -drawRect: or -drawTextInRect: to a UILabel (see: Is it possible to change the distance between letters / Kerning font using Cocoa Click? ). This seems like a crazy solution and would be prohibitively difficult if a string mixes monospaced and non-monospaced fonts.

There are also a number of questions that point, in particular, to numbers, creating a font with a font descriptor with certain font features enabled. (see Display proportionally spaced numbers (instead of monospaced / tabular) in iOS ). This seems to work only in fonts that include these features. The font I use does not include these features, and they have no effect. Font functions can be tested using something like NSLog(@"%@", CTFontCopyFeatures ( ( __bridge CTFontRef ) myFont ));

+5
source share

Source: https://habr.com/ru/post/1214335/


All Articles