Automatically adjust font size in Godot to fit text in a label?

  • Thread starter Darkmisc
  • Start date
  • #1
Darkmisc
203
27
TL;DR Summary
I'd like my text to always fit on one line within a label. Is there a way to do this automatically?
Hi everyone

So far as I can tell, there is no function to automatically shrink text to fit on one line within a label in Godot 4. Instead, I'm going to get the length of each string with
len(string)

and use
Label.set("theme_override_font_sizes/font_size", size)

to adjust the size according to the length of the string. However, I think this will lead to the font size sometimes being smaller than it needs to be. Is there a better way of doing it?

I wanted to measure the width in pixels for a given string (at a given font size), but I'm not sure that function exists either.

Thanks
 
Technology news on Phys.org
  • #2
Godot 4 seems to be some sort of dev framework. But what langauge does it use?

It seems pretty specific. You might have better luck asking your question on Stack Exchange, which is designed for such language-specific questions and answers.
 
  • Like
Likes Darkmisc
  • #3
GDScript
 
  • #4
Since most fonts in use on an UI are proportional using just the length of your character string and general font info will, as you notice, not provide a particular accurate result. Many UI frameworks provide as part of their text rendering capability a function that given a string and some font metrics can return values such as bounding box and baseline info, so you may want to search to see if Godot offers a scripted version of that (I am not familiar with Godot nor what text rendering API is available for its GDScript/C# language variants).

Regarding using font size to ensure that individual UI items uses a lesser font size until the text is fully rendered may result in your UI ends up looking like a word cloud diagram if taken to the extreme. The general approach for visibility of text in a balanced matter can be a complicated design process where both different texts and (type of) elements are tried out to find something that pass UX muster. A significant design constrain for this process typically is where on the scale from full size mouse-and-keyboard-operated UI application down to small screen touch-only mobile-like apps. If you are doing a game it probably sits somewhere around the middle of this scale, but even here the might prove hard to put "general" text on the UI without some kind of clunky compromise.
 
  • Like
Likes Darkmisc and DaveC426913
  • #5
As a former UI Specialist I fully concur with FP's analysis. It can be disconcerting to have more than a few font sizes on a screen. If you want it aesthetically pleasing, consider keeping the font sizes similar (for similar levels of functionality, just like you would in a document) and using centre-justify to fit the text in the space.
 
  • Like
Likes Filip Larsen and Darkmisc

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Cosmology
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Sci-Fi Writing and World Building
Replies
9
Views
2K
Replies
10
Views
2K
Replies
9
Views
3K
Replies
2
Views
1K
  • Sci-Fi Writing and World Building
2
Replies
52
Views
4K
Replies
152
Views
4K
Back
Top