Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIFont.monospacedDigitSystemFontOfSize() not really monospaced?

I wanted to use SF's monospaced digits font to display an integer number in a text field by changing its font as follows:

textField.font = UIFont.monospacedDigitSystemFont(textField.font!.pointSize, weight: UIFont.Weight.semibold)

But if I set the text of the text field in a 60Hz frequency, this is the result:

SF monospaced

The width of the text is clearly not constant for a same amount of digits so it is moving all jittery because the text field is constrained to "leading" and "trailing" of the image underneath. Why is this the case and how to fix it?

Another truly monospaced font like "Menlo" is behaving correctly:

Menlo

like image 592
YourMJK Avatar asked Jul 01 '16 14:07

YourMJK


People also ask

What font is monospaced?

Examples of monospaced typefaces include Courier, Roboto Mono, Inconsolata, Source Code Pro, and IBM Plex Mono.

Why is code in monospace?

Developers almost always use a monospace font because we don't read code in the same way we read text. You can glean the meaning of a large block of text by briefly looking at pieces of the words and how they fit together.

What monospace means?

(ˈmɒnəʊˌspeɪst ) adjective. (of printed material) having been printed in a font in which all characters have the same breadth.

Is monospace better?

Compared to proportional fonts, monospaced fonts are harder to read. And because they take up more horizontal space, you'll always get fewer words per page with a monospaced font. In standard body text, there are no good reasons to use monospaced fonts. So don't.


1 Answers

So it seems like setting the "adjust to fit" option of the text field is overwriting the monospaced property of the font (even if the view is big enough to contain the text!)

My temporary solution at this point is to

  • Set textField.adjustsFontSizeToFitWidth to false
  • Set the monospaced font again (as it was somehow overwritten by adjustsFontSizeToFitWidth's setter)
  • Begin to change the text
  • When finished set textField.adjustsFontSizeToFitWidth to true again in order to correctly behave on user input

This is not what I originally wanted but it works as a workaround

enter image description here

like image 87
YourMJK Avatar answered Sep 23 '22 18:09

YourMJK