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

  • Thread starter Thread starter Darkmisc
  • Start date Start date
AI Thread Summary
In Godot 4, there is currently no built-in function to automatically shrink text to fit within a label on a single line. Users can manually adjust the font size based on the string length using the len(string) function and Label.set("theme_override_font_sizes/font_size", size), but this method may not always yield optimal results. There is a need for a function to measure the pixel width of a string at a specific font size, which is not readily available. The discussion highlights that relying solely on string length for font size adjustments can lead to inaccuracies, especially with proportional fonts. It is suggested that a more sophisticated approach, similar to those found in other UI frameworks, would be beneficial. Additionally, maintaining consistent font sizes across UI elements is emphasized for aesthetic and usability reasons, particularly in game design where a balance between visibility and design is crucial.
Darkmisc
Messages
222
Reaction score
31
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
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.
 
GDScript
 
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
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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
2K
Replies
4
Views
3K
Replies
4
Views
4K
2
Replies
52
Views
7K
Replies
152
Views
9K
Replies
3
Views
2K
Replies
2
Views
9K
Replies
2
Views
3K
Replies
2
Views
4K
Back
Top