Draw text to fit within the canvas or a rectangle

JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

I need to draw text to fill (i.e. be the largest sensible size which fits in) a rectangle. How can I do this? I can't see any methods to scale/fit the text - so I'm assuming (hopefully incorrectly) that I'm going to have to set a font size and measure to see if it fits, and adjust the font size accordingly.

Best Answer

  • JamesLaveryJamesLavery GB ✭✭✭✭✭
    Accepted Answer

    I came up with a slightly inefficient method for doing this starting with a small TextSize and increasing it and measuring until it doesn't fit in the rectangle - then taking the last size which fits.

    To make this efficient, I then cache the TextSize for a given control size.

    Not ideal, but it works well!

Answers

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Accepted Answer

    I came up with a slightly inefficient method for doing this starting with a small TextSize and increasing it and measuring until it doesn't fit in the rectangle - then taking the last size which fits.

    To make this efficient, I then cache the TextSize for a given control size.

    Not ideal, but it works well!

Sign In or Register to comment.