font resizing algortihm

A

ajk

I have a user control which displays a number say 12.2 and I would
like to maximize the size of the number in the control. If a user
changes the size of the control the number/font size should scale
accordingly.

At the moment I create a number of Font's; first large then smaller
until it fits into the control, then from small to large until it no
longer fits - then take largest fitting font size. This seems somehow
like a resource demanding clumsy way so I was wondering if somebody
knows a best practice for this?

br/ajk
 
C

Clayton

I have a user control which displays a number say 12.2 and I would
like to maximize the size of the number in the control. If a user
changes the size of the control the number/font size should scale
accordingly.

At the moment I create a number of Font's; first large then smaller
until it fits into the control, then from small to large until it no
longer fits - then take largest fitting font size. This seems somehow
like a resource demanding clumsy way so I was wondering if somebody
knows a best practice for this?

br/ajk

hi,

i've seen a good algorithm on Que's book for exam 70-316. when i find
it i post back here
 
P

Peter Duniho

ajk said:
I have a user control which displays a number say 12.2 and I would
like to maximize the size of the number in the control. If a user
changes the size of the control the number/font size should scale
accordingly.

At the moment I create a number of Font's; first large then smaller
until it fits into the control, then from small to large until it no
longer fits - then take largest fitting font size. This seems somehow
like a resource demanding clumsy way so I was wondering if somebody
knows a best practice for this?


It _is_ resource demanding and a bit clumsy. But it's pretty much what
you have to do.

You can make a pretty good initial guess by finding the pixel size of
some "standard" font size, and then scaling that according to the ratio
of the size of the space in which you want the displayed text to fit to
the size that you calculated. But for a perfect fit you will still need
to test the calculated size and refine it based on the actual metrics.

IMHO, if you want the least clumsy code and are willing to accept a
situation where the font could be slightly larger but isn't, you should
just calculate the ratio-based size, and then reduce it slightly
iteratively until it fits. That at least avoids having the extra case
of dealing with when it's too small (which is subtly different from the
case when it's too big, requiring some almost-the-same-but-not-quite
duplicated code).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top