5

Is it possible to force a UIFont to be monospaced?

Specifically I'm using a font which does not contain monospaced numbers (tabular numerals). Creating a modified font object which is monospaced and adding that font to the numeric segments of attributed strings would work great.

Another solution may be to add custom attributes to an attributed string and modify what is handling the layout of the text to use a fixed size for glyphs in particular ranges.

Things that Haven't Worked:

There are a number of questions that propose solving similar problems by overriding -drawRect: or -drawTextInRect: on a UILabel (see: Is it possible to alter the letter-spacing/kerning of a font with Cocoa Touch?). This seems like an insane solution and would be prohibitively complex if a string mixes monospaced and not-monospaced fonts.

There are also number of questions which suggest, specifically in regards to numerals, creating a font with a font descriptor with certain font features enabled. (see Displaying proportionally spaced numbers (instead of monospace / tabular) on iOS). This seems only to work in fonts which include these features. The font I'm using does not include these features and they have no effect. The font features can be checked by using something like NSLog(@"%@", CTFontCopyFeatures ( ( __bridge CTFontRef ) myFont ));

Community
  • 1
  • 1
Anthony Mattox
  • 7,048
  • 6
  • 43
  • 59
  • "Creating a modified font object which is monospaced and adding that font to the numeric segments of attributed strings would work great." Why don't you do this? (Just include the modified font in your app bundle.) – Aaron Brager Feb 27 '15 at 16:09
  • 1
    @AaronBrager Excellent point. 1) I don't have the software or expertise to modify the font. 2) I feel like there out to be a way to 'create' a modified font object programmatically which would save some space in the bundle. – Anthony Mattox Feb 27 '15 at 16:14
  • https://www.fiverr.com/bennathan/help-you-with-your-font-file – Aaron Brager Feb 27 '15 at 16:58
  • 1
    Point well taken, @AaronBrager. I'm still interested in finding out if there is a programatic solution to this though. – Anthony Mattox Mar 02 '15 at 15:52

0 Answers0