Hello @ all,

I would like to display in a label taxt with the maximum font size. So that all characters are still readable. I have already found a solution which works. However, my code (exported to an Android phone) needs a small eternity during execution and is not usable.

		fragenLabel.visible = false
		fragenLabel.text = new_text
		dynamic_font_size = 112
		dynamic_font_frage.size = dynamic_font_size
		fragenLabel.add_font_override("font", dynamic_font_frage)
		
		while (fragenLabel.get_line_count() > fragenLabel.get_visible_line_count()):
			dynamic_font_size -= 2
			if (dynamic_font_size <= 42):
				break
			dynamic_font_frage.size = dynamic_font_size
			fragenLabel.add_font_override("font", dynamic_font_frage)
		
		fragenLabel.visible = true

Does anyone have any other ideas? I am thankful for any tip, best regards cya Thommy

A dynamic font size of 112 is huge and will take a long time to generate glyphs for new characters on mobile. On top of that, you're continuously changing the DynamicFont size and adding font overrides, which may cause Godot to render dozens of font sizes you'll never see. Look into fixing that first.

If that still doesn't solve the issue, you need to either pre-render the font on startup by displaying all ASCII characters for one frame in a label then hiding this label, or use smaller font size but scale the Control up using the rect_scale property (which will look blurry).

Note that setting rect_scale in a Container won't work if you do it in the editor – you have to do it at run-time using a script, with an one-frame delay if it's done in _ready():

set_deferred("rect_scale", Vector2.ONE * 2)

@Calinou

Glyphs that large might also be a memory hog.

My concern would be if the typeface is kerned, fixed font or not etc. Because he's scaling up text and testing line count changes, I am assuming he's got wrapping on, he might be better off using font metrics to estimate the point size

Also if the text itself is constant and isn't subject to change, the appropriate point sized should be derivable before hand with font metrics.

I'm thinking about this for my project, which has a lot of text. My idea right now is to support 3 target resolutions, for example 720p, 1080p, and 1440p. Then I just check the viewport size (I can listen to a signal), do a comparison for my low/medium/high and then change the font sizes. The font changing logic would only (ideally) happen once at launch for most people. But on mobile people can change the orientation, but this doesn't happen often (same with PC manually changing resolution). Could be a good idea.

@cybereality said: I'm thinking about this for my project, which has a lot of text. My idea right now is to support 3 target resolutions, for example 720p, 1080p, and 1440p. Then I just check the viewport size (I can listen to a signal), do a comparison for my low/medium/high and then change the font sizes. The font changing logic would only (ideally) happen once at launch for most people. But on mobile people can change the orientation, but this doesn't happen often (same with PC manually changing resolution). Could be a good idea.

With Godot's 2d or viewport stretch mode, you do not need to scale things yourself. Just configure UI anchors correctly and multiple resolutions and aspect ratios will be supported out of the box. When using the 2d stretch mode, DynamicFonts will be oversampled by default to remain crisp at all resolutions (you can disable this in the Project Settings).

Yeah, I tried that but it doesn't work for my use case. It looks okay if I just enlarge the window, but making the window smaller or changing the aspect ratio will shrink text to become too small to read (like on mobile). It's probably fine for a HUD on a 2D/3D game, but my project is more like a visual novel, so it's important that the text is a specific size.

Here is an example of what I'm saying.

Enlarging the window (while staying 16:9) works as does widening the width:

But if you shorten the width, the text does not expand vertically, it just gets smaller.

Maybe I missed a setting somewhere, but it's not working for me.

I've attached the project too if you want to take a look.

@cybereality said: Here is an example of what I'm saying.

-snip-

Enlarging the window (while staying 16:9) works as does widening the width:

-snip-

But if you shorten the width, the text does not expand vertically, it just gets smaller.

-snip-

Maybe I missed a setting somewhere, but it's not working for me.

I've attached the project too if you want to take a look.

Set the project resolution to be a square resolution that matches the lowest dimension. For instance, if you intend to develop for 1280×720 and want both portrait and landscape mode to look correctly scaled, use 720×720 as the default window size in the Project Settings. Then make sure the stretch aspect is set to expand instead of keep.

Of course, this means you have to make sure the UI can always fit in a 1:1 aspect ratio. Depending on the kind of game you're making, this can be easier said than done :) If you need to work around this, you could use a script to load different UI layouts depending on the window's aspect ratio.

See https://godotforums.org/discussion/comment/48187 which precisely talks about the same issue.

Actually, that seems to work. I was even in that other thread but I didn't realize that was the solution. Still need to test a few more things, but that seems like it. Thanks.

Thanks again @Calinou . That trick saved me from writing some sketchy code. It works perfect, just tested it on my phone. The only thing I had to customize was adding an AspectRatioBox and limiting it to 16:9 (so the UI wasn't too big on ultrawide monitors). But for mobile I had to check the resolution and switch it to 9:16 in portrait mode. But this works fine , I haven't noticed any issue.

5 days later
2 years later