#Can I make a Label/RichTextLabel get smaller instead of expanding/warping?

1 messages · Page 1 of 1 (latest)

thin tartan
#

Put simply, I have a RichTextLabel that shows my card'sname. One problem, I dont have a lot of room to work with. The text can wrap one line just fine, but any more thant that and you'll have to scroll. Ideally, the font size should shrink instead of wrappin the text. Any way I can achieve this?

north dragon
#

I had this exact same problem and was very frustrated by it. As far as I can tell you can't solve this without coding the behaviour yourself.

#

I will post mine in a bit, sorry, got busy

north dragon
#

@thin tartan here's my code for doing it

#
public partial class FancyLabel : RichTextLabel
{
    private float MaxHeight;
    private bool Initialized;

    private void Initialize()
    {
        ScrollActive = false;
        BbcodeEnabled = true;
        MaxHeight = Size.Y;
        FitContent = true;
        MouseFilter = MouseFilterEnum.Ignore;
    }

    public void SetRichText(string text, bool centered = true)
    {
        if (false == Initialized)
        {
            Initialized = true;
            Initialize();
        }

        if (centered)
        {
            text = text.RichWrap("center");
        }
        
        Text = text;
        UpdateFontSize();
    }
    private void UpdateFontSize()
    {
        var fontSize = GetThemeFontSize(ThemeConstants.FontSize);
        while (fontSize > 1)
        {
            
            if ( Size.Y <= MaxHeight )
            {
                break;
            }
            fontSize--;
            SetFontSize(fontSize);
        }
    }
    
    public void SetFontSize(int size)
    {
        AddThemeFontSizeOverride(ThemeConstants.FontSize, size);
    }

#

and I just use the SetRichText() to set the text of the label, instad of just assigning it to the Text field

#

it's in c#, hopefully it's clear enough to be translated into gdscript easily