Get the real size of a Label

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MarcPhi

Hello is there a way to get the real size of a label, when the text is clipping above the normal bounderies or is there a ways to check if the text clips)?

Hello, I do not understand very well what you want to do, but on the label you can activate the Autowrap option, so that the text is not cut.

Aquiles | 2019-10-29 17:14

hey thats exactly what i did, but i want to know when the text uses autowrap so i can change the text size accordingly

MarcPhi | 2019-10-29 18:05

a slightly crude solution, it would be to see how many characters enter the specific width and then compare with $Label.get_total_character_count() that will return you a number of characters.

Aquiles | 2019-10-29 22:13

:bust_in_silhouette: Reply From: Adam_S

You could use the resized() signal, and get the size of the Label with $Label.rect_size. The properties “Clip text” and “Autowrap” have to be off for this to work.

EDIT:

Since you want to have autowrap on, you need to check the size (in pixels) of your longest word:

func check_word_size():
    var font = $Label.get_font("font")
    var words = $Label.text.split(" ",false)
    var longest_word = ""

    for i in words:
        if font.get_string_size(i).x > font.get_string_size(longest_word).x:
            longest_word = i

    if font.get_string_size(longest_word).x > $Label.rect_size.x:
        # WORD IS BIGGER THAN THE LABEL'S RECTANGLE
        pass

the problem with that solution is that i would need to split the text in lines on my own

i would like to have autowrap on but:
Imgur: The magic of the Internet

MarcPhi | 2019-10-30 18:47

Just updated my answer.

Adam_S | 2019-10-31 04:40

:bust_in_silhouette: Reply From: snakefoot
theme.default_font.get_string_size( $Label.text )

will give you the size of the text as a Vector2.

$Label.autowrap = true
$Label.rect_size.x = 1
while( $Label.get_line_count() > 1 ):
 $Label.rect_size.x += 1

will resize the Label until the text fits in one line.

You could use a similar method to shrink the font size, but this very easily leads to font size zero if you don’t know how long your String could potentially be.

1 Like