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

  • Thread starter Darkmisc
  • Start date
  • #1
Darkmisc
213
28
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

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

How can I automatically adjust the font size to fit text within a Label in Godot?

To automatically adjust the font size to fit text within a Label in Godot, you can use a script that iteratively reduces the font size until the text fits within the Label's dimensions. This involves checking the text's width against the Label's width and adjusting accordingly.

Is there a built-in feature in Godot for automatically adjusting font size to fit text?

No, Godot does not have a built-in feature specifically for automatically adjusting font size to fit text within a Label. You will need to implement a custom solution using GDScript to achieve this functionality.

Can you provide a sample GDScript to adjust the font size automatically in Godot?

Yes, here is a simple example of a GDScript to adjust the font size automatically:

extends Labelfunc _ready():    adjust_font_size()func adjust_font_size():    var font = self.get("custom_fonts/font")    var target_width = rect_size.x    var font_size = font.size    while font.get_string_size(text).x > target_width and font_size > 1:        font_size -= 1        font.size = font_size
This script reduces the font size until the text fits within the Label's width.

Does adjusting the font size affect the readability of the text in Godot?

Yes, adjusting the font size can affect the readability of the text. If the font size becomes too small, it may be difficult for users to read the text. It's important to find a balance between fitting the text within the Label and maintaining readability.

How can I ensure that the text remains readable while fitting it within the Label?

To ensure that the text remains readable while fitting it within the Label, you can set a minimum font size threshold. This way, the font size will not be reduced below a certain point, ensuring that the text remains legible. Additionally, consider other layout adjustments such as increasing the Label's size or using multi-line text to accommodate longer strings.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Cosmology
Replies
4
Views
2K
  • 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
5K
Back
Top