Here's Tim's Autosize(R) Techonology as posted by the main man:
I have two versions of autosize - one just sizes a field vertically, one
both ways for popup type boxes (using a hard coded ratio of 2.5 to 1 - I
HATE hard coded things like this, but what can you do?). I imagine its the
latter you're after. The former uses a neat binary algorithm to get there
real fast. The latter one isn't that sophisticated - it also is only setup
(currently) for a fixed height font:
to get autoSizeField fieldRef
textHeight = system_textHeight()
widthInc = 2.5 * textHeight
scroll of fieldRef = 0
popWidth = widthInc
popHeight = textHeight
size of fieldRef = popWidth, popHeight
while textOverflow of fieldRef > 0
increment popWidth by widthInc
size of fieldRef = popWidth, popHeight
if textOverflow of fieldRef = 0
size of fieldRef = popWidth, popHeight - textHeight
if textOverflow of fieldRef > 0
size of fieldRef = popWidth, popHeight
end
break while
end
increment popHeight by textHeight
size of fieldRef = popWidth, popHeight
end
end
Significant features:
field size is cached, so we're always setting it, never getting it -->
speed improvement
Increases width first - then checks if all text fits and breaks out if it
does, before increasing height
If breaks out after increasing width, tries decreasing height to see if
text still fits (otherwise you sometimes get a blank line after the text)
To add - if break out after adjusting the height, try decreasing the
width as far as possible - occassionally you get a lot of space to the right
(if there are lots of CRLFs or something).
If you wanted to handle any fontsize (or a variety of fontsizes) gracefully,
I'd do the following (binary technique):
Start at an arbitrary minimum size (600x240 is nice :-)
Double the width and height until textoverflow is zero
You now have upper and lower limits to use to begin narrowing down using
a binary technique - this is very fast
Tim.
---------------------------------------------------------------------------
Tim Barham
tbarham@vcrpmap.telecom.com.au
---------------------------------------------------------------------------
Revised 8/8/95 brianp@u.washington.edu
(Brian Parkhurst)
University of Washington
Box 357260
Seattle, WA 98195-7260
206/543-9175