Get default font size

I leave the font size on every control as the default 0. However, I’m looking to find the actual value the system is using for this value, in order to pass it over as a variable to a method. Is there any way to detect what size the system is actually using?

Everywhere I look at the fontsize property Xojo always returns 0 - text field, text area, canvas, listbox.

This is for a cross platform desktop app.

Thanks in advance.

Usually size 0 corresponds to size 12.

This method should calculate the actual pt size of the default font size setting:

Public Function calculateDefaultSize() As Double
  var surface as new Picture( 10, 10 )
  var defaultHeight as Double = surface.Graphics.TextHeight
  
  surface.Graphics.FontSize = 0
  
  var currentHeight as Double
  while currentHeight < defaultHeight
    surface.Graphics.FontSize = surface.Graphics.FontSize + 1
    currentHeight = surface.Graphics.TextHeight
  wend
  
  Return surface.Graphics.FontSize
End Function

You may want to code in a sanity check that causes it to exit the loop if the value of currentHeight gets obscenely large, just in case something goes wrong, but it may never be needed.

2 Likes

That looks like a really neat solution - thanks @Anthony_G_Cyphers !

Happy to help. If it works for you, please don’t forget to mark it as the solution.

I think on macOS it is more like 13.

As you can see with NSFontMBS class:

MsgBox str(NSFontMBS.systemFontSize)

Why do you need the exact value? If you are drawing into a Graphics object, you should be able to set TextSize=0 to have it use the default size.

1 Like

I’m using it to size a number of elements to 2x or 3x the text size, which on Linux particularly differs for any number of reasons and allows the user to set a default within the settings.