Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the .monospaced system font design

Tags:

I am attempting to use a system font and apply the monospaced design, without luck. I can successfully make the text monospaced using the custom font function and passing in Courier and a size, but this is not idea because then the font size is fixed.

enter image description here

VStack {
    Text("lmlmlmlm 12345678")
    Text("lmlmlmlm 12345678")
    .font(Font.system(.body, design: .monospaced))
    Text("lmlmlmlm 12345678")
    .font(Font.custom("Courier", size: 18))
}

How do I get the system font to work with the .monospaced design? I think it might be a bug with .monospaced, because the .serif option does modify the text as expected.

like image 562
DLAN Avatar asked Jun 15 '19 20:06

DLAN


People also ask

How do I change the font in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.

Where do you put the monospace font?

Monospaced fonts are customary on typewriters and for typesetting computer code. Monospaced fonts were widely used in early computers and computer terminals, which often had extremely limited graphical capabilities.

Are monospaced fonts 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.


2 Answers

It seems .monospaced font only applies when given a fixed size:

Text("monospaced")
  .font(.system(size: 14, design: .monospaced))

This won't work given a dynamic text style such as body. But as you've also mentioned it works fine for other fonts so this is probably a bug in Xcode 11.0 beta and hopefully will be fixed in next releases.


Update:

This issue was fixed with Xcode 11 beta 3. The following code works now:

Text("monospaced")
    .font(.system(.body, design: .monospaced))
like image 156
M Reza Avatar answered Sep 19 '22 02:09

M Reza


In case you want to make only digits monospaced, you might try using something like this:

Text("0123456789")
    .font(Font.system(.body, design: .monospaced).monospacedDigit())

This does not circumvent the obvious bug of Xcode 11.0 beta, however. Letters are still not rendered monospaced.

like image 29
Maki Avatar answered Sep 20 '22 02:09

Maki