Retrieving true fontsize in a graphics paint event


I would like to increase of decrease the fontsize when drawing text in a canvas.

But the size (default) of g.FontSize is 0. So it’s the best defined by the operating system and the scaling.

The documentation states the following

If you want text to be shown using the user’s System font, use the name “System” as the font when you assign it. You can enter it as the TextFont property in the Inspector. If you also enter zero (0) as the font size, the font size that works best for the platform on which the app is running is used. Because of differences in screen resolutions, different font sizes are often required for each operating system platform. This feature enables you to use different font sizes on different platforms without having to create separate windows for each platform. Use the Inspector to set the font name to “System” and the font size to zero (0).

How do I retrieve the real value of FontSize ? I would like to increase by few points or decrease by few points this value to draw a title or a description.


Short answer: I suggest that you just set the FontSize to 12, and then adjust it from there.

Long answer:
When FontSize is zero, it is somewhere close to 12, but depends upon the actual operating system. I solved this problem in my own apps by simply setting the FontSize. This prevents having to figure out the FontSize, and makes the app behave the same across platforms.

Longer addendum:
You can use Graphics.TextHeight and Graphics.TextWidth to get the actual metrics of text (using the current Graphics.FontName, Graphics.FontSize, and Graphics.FontUnit values).

Hope that helps.


Thank you for the answer ! I tried something to compare the FontSize between different controls

On the paint event of the DesktopCanvas and the DesktopContainer, I got a real value of 12 on the FontSize but on the PaintCellText of the DesktopListbox, I got 0

Why is the API not consistent with the value returned from different controls?

Also what is the difference in the inspector between FontSize and TextSize in the Graphics object ?


I can’t speak to the API as I didn’t write it. :smile: However, the API is correctly returning the value of zero when that is the value that is actually set in the Inspector. But the value “zero” is actually used as an internal flag to tell Xojo “use the default font size for this operating system.”

For the record, I experienced this same frustration some time ago. I think it would be less confusing if Xojo didn’t overload the FontSize value of zero for use as an internal flag, and simply set the default to 12 (which is what I end up doing in nearly every text control I add).

I’m assuming you meant “TextHeight” (and not “TextSize”) … FontSize is a “getter/SETTER” that lets you assign the font size. TextHeight is a method that GETS the size of a text BLOCK. In other words, TextHeight can measure the size of multiple lines of text, as well a single line.

Thank you for the answers.

For TextSize, I do not assumed TextHeight that returns something.

This is looking into a default Graphics of a DesktopCanvas (Paint) and a default Graphics of a DesktopListbox (PaintCellText)

Canvas (Object g as Graphics in Paint event):

DesktopListbox (Object g as Graphics in PaintCellText event):

I decided to create a method that generate a picture and get the FontSize from the Graphics object. I am still testing it on multiple OS to see if it’s consistent or not. For the moment, it returns 12 in most cases.

Ah … a quick look at the inspector for “g” (by typing “g.” + [Tab]) … and it shows that TextSize is just a deprecated alias for FontSize …

Screenshot 2023-10-03 at 7.37.16 AM

Oh I do not have the deprecated showing thats why, my bad

For Windows and Linux the default control font size was historically 10pt, while macOS used 12pt. I think (though haven’t verified) that Windows now uses 11pt. So it’s typical to have code that’s something like the following for determining which size to use:

if g.FontSize = 0 then
  #if TargetMacOS then
    g.FontSize = 12
    g.FontSize = 10
end if