Shrink/grow to fit

Is there an out-the-box way of scaling text to fit a rectangle that doesn't change the aspect ratio of the text?

 

void drawTextToFillRectangle (Graphics & g,

                              const Font & font,

                              const String& text,

                              float x, float y,

                              float width, float height)

{

    float currentHeight = font.getHeight();

    float currentWidth = font.getStringWidthFloat(text);

   

    float scale = jmin(height / currentHeight, width / currentWidth);

    Font newFont(font.withHeight(scale * currentHeight));

   

    g.setFont(newFont);

   

    g.drawText(text, x, y, width, height, Justification::left, false);

}

 

Trivial answer....maybe should have had that coffee first.

2 Likes