Changing font size of theme or control at runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CalmTurtle

I would like to change font size of a theme. Looking through the documentation I can figure out the gets, but don’t see an applicable sets to adjust a font (and thus its size) during runtime.

End state is having a player be able to adjust font size for easier viewing. I could do this by scalers, but seems to be a clunky way (scale then adjust positions and offsets, etc.).

There have been a few answers to similar questions, but those seem to only apply to earlier releases.

Thanks in advance.

:bust_in_silhouette: Reply From: Zylann

Font size is not a property of the theme, it’s a property of Font. Or more specifically, DynamicFont. So to change the font size of all your controls, you have to get the font (from the theme, from the control, or just load it) and set its size property.

If you want this to be applied only to specific controls, you may want to make another version of that font (but using the same DynamicFontData), or make a duplicate in code, set its size and add a font override to the control. In code, that’s done with add_font_override().

In regard to the above, if a player wants to change fonts size of the game, you’d have to get all dynamic fonts in your project and set their size.

Note that if you did use multiple fonts in your project, you may want to keep them loaded when the game runs by maintaining a reference to them, so the change will persist. The reason is, in Godot, resources that are not referenced by anything are unloaded, which would make the size override to revert to what it was in the resource file. However if you have only one main font referenced in your Theme, you won’t need this.

Thanks for the note. Using the above I ended up with the following code that worked out for me:

var font = $YourControlNode.get_font("string_name", "")
font.size = 20
$YourControlNode.add_font_override("string_name", font)

Note: the value of string_name does not have an effect that I can see.

CalmTurtle | 2019-03-24 17:25

The string name has no effect on most controls probably, but I think it’s there for controls that may render text using multiple fonts.

Zylann | 2019-03-24 17:28

var font = $YourControlNode.get_font("string_name", "")
font.size = 20

add_font_override() is not needed if you only want to change font size of $YourControlNode

1shevelov | 2021-01-23 04:58

Thanks for the hint! In c#:

var fontForExplanation = rtlExample.GetFont("normal_font","");
(fontForExplanation as DynamicFont).Size = 24; 

GetFont() returns Font which is forefather of DynamicFont and BitmapFont

zbyna | 2022-04-10 03:50

Thanks for this answer in C#, it worked for me. I could not find any documentation.

henrycase | 2022-09-27 17:26