Any suggestions for maintaining a font scale inside a resizable
rectangle?
Because font glyphs have to be an integral number of pixels wide, you
are unlikely to find a font size such that the text will fit exactly
within the specified width. What you should do is figure out an
approximate scale factor as a first step:
ScaleFactor := MyRect.Width / TextWidth(...);
Then multiply the Height of the font by this factor to get a new font
height:
Font.Height := Round(ScaleFactor * Abs(Font.Height));
And then iteratively test the width of the resulting text and decrease
the font height one pixel at a time until the text fits:
W := TextWidth(...);
while W > MyRect.Width do begin
Font.Height := Abs(Font.Height) - 1;
W := TextWidth(...) end;
-Steve
Thanks. I am already scaling things. I thought the iterative process
seemed inefficient and maybe there was a trick I should know, but it sounds
like there isn't. I thought of the iterative algorithm also. Only I tried a
couple of others first :-)
I guess that means there aren't any nice tricks on this one.
> I guess that means there aren't any nice tricks on this one.
The reason there aren't any tricks is that it is essentially
impossible to predict how a glyph width will "round off" when you
change the font the size slightly.
-Steve