0 votes

I would like to adjust the font size in a label so that the entire text is always displayed. My first approach was to simply count the lines and determine the size. If the size is then larger as the label I wanted to reduce the font size.

If I start with


I always get an outdated value back.

I set the text with :

label.text = str_text


label.settext( strtext)

Am grateful for any tips and tricks, even if there is still a more elegant method for my problem. ;)

Thx and cya Thommy

Godot version 3.2.3
in Engine by (15 points)

1 Answer

0 votes
# Make a dedicated class, so you don't have to write this code again, (Didn't test this, but should be able to give the idea)
# This will overwrite the labels font data so you would need to add support for things like colors and etc

# Adding this line, enables tool, which allows your changes to reflect within editor, without having to run the game, but can cause funny things and even crash the editor when used incorrectly

class_name ResizableLabel

extends Label

const FONT_FALLBACK := preload("path to any font")
const PROPERTY_FONT := "custom_fonts/font"

export var size := 16 setget set_size
export var font := FONT_FALLBACK setget set_font

func _init() -> void:

func set_size(new_size : int) -> void:
  size = new_size

func set_font(new_font : DynamicFontData) -> void:
  font = new_font if new_font else FONT_FALLBACK

func _resize() -> void:
  var dynamic_font := DynamicFont.new()
  dynamic_font.font_data = font
  dynamic_font.size = size
  set(PROPERTY_FONT, dynamic_font)

# You can now see and instance this class through "Create new node" popup and be able to reuse it, just like built in nodes

Although this approach is quite unconventional, would suggest trying to use autowrap and scroll container, for really long texts

Or setting a dedicated size and a maximum amount of texts

Or line edit/text edit for inputs

by (390 points)
edited by

I'll try it right now and let you know if it worked.
Thank you and best regards from Hamburg

cya Thommy

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.