UIFont.monospacedDigitSystemFontOfSize() not really monospaced?
Asked Answered
E

4

17

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

Erlond answered 1/7, 2016 at 14:35 Comment(3)
Same thing also happens to a UILabel constrained in its width (with "adjust to fit" enabled)Erlond
You are using Xcode 8 and Swift 3 I assume?Adularia
@CodeDifferent No, Xcode 7.3.1 and Swift 2Erlond
E
9

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

Erlond answered 1/7, 2016 at 18:20 Comment(3)
@matt You weren't really "the one who put me onto this", I figured out that "adjust to fit" changes the whole font and not the size. But I still really appreciate your efforts! Notice that this isn't the accepted answer, my original problem is still left. I just wanted to give an answer to where the problem comes from and present an optional solution. And I am considering writing a bug report to AppleErlond
It wouldn't have killed you to upvote my answer. I spent time on this; I even made a whole project to prove that "adjust to fit" was the problem. People are not going to have any incentive to help you if you don't reward them.Kal
Disabling "Adjust to fit" in Xcode 9 solved the problem for me.Mezzanine
H
2

I have a project that uses the "adjust to fit" option and if I set the monospaced property using didSet for the label outlet, it seems to work for me in Xcode 8 Beta 3.

@IBOutlet weak var textLabel: UILabel! {
    didSet {
        textLabel = UIFont.monospacedDigitSystemFont(ofSize: textLabel!.pointSize, weight: UIFontWeightRegular)
    }
}
Hamper answered 1/8, 2016 at 18:32 Comment(1)
Thanks, I will try that out! But shouldn't it be "textLabel.font =" and "textLabel!.font!.pointSize"?Erlond
G
0

In my case, I was setting the font property of a UILabel to a monospaced digit font and using attributedText to set the text. This worked in iOS 9 but broke in iOS 10. The workaround is to explicitly set the font attribute in the attributed string.

Grewitz answered 10/1, 2017 at 0:9 Comment(0)
P
0

I was having the same problem and did set code worked. Updated for Swift 4:

@IBOutlet weak var textLabel: UILabel! {
    didSet {
        label.font = UIFont.monospacedDigitSystemFont(ofSize: textLabel!.font!.pointSize, weight: UIFont.Weight.regular)
    }
}
Pennypennyaliner answered 21/1, 2018 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.