Yeah, so the brute force way is to iterate through sizes till it fits tightly …not quick, but does the job.
I don’t think there’s an easy way of doing it…
Edit: Unless you somehow use Path and Font::getOutlineForGlyph(), where the font size is 1.0f (for easy scaling of the paths). With a bit of math, it could be faster than my first suggestion.
here’s a couple of functions i use. note that you can also affect the horizontal scale when making a fitting.
Note, here `_font’, you’ll have to pass in or get from your class as i do here.
[code] void setFontSizeForBounds(int w, int h, const String& text, int margin = 0)
{
_font.setHeight(h);
_font.setHorizontalScale(1);
// round up to pixel
int fw = _font.getStringWidthFloat(text) + 0.5f;
if (fw + margin > w)
{
float s = (w - margin)/(float)fw;
h = h*s; // round down to pixel
}
setFontHeight(h);
}[/code]
and this one does the same (without a margin) but with a min horizontal scale