Auto-resizing text boxes

Adam Spitz adspitz at
Sun Feb 23 01:39:32 UTC 2003

I'd like to have a text box that can resize itself so
that all of its text fits inside it (without
wrapping). I've seen the autoFit stuff on TextMorph,
but it doesn't seem to do what I want. So I added the
following hack to PluggableTextMorph:

	| font longest desiredWidth |
	"I'm sure this isn't the right way to do
	this - in particular, what if the font
	isn't the same for the entire text? -
	but it's good enough for now."
	font := self text fontAt: (self text fontNumberAt: 1)
withStyle: TextStyle default.
	longest := 100.  "Don't go below 100."
	self text asString linesDo: [:line | longest :=
longest max: (font widthOfString: line) ].
	"Leave a bit of extra room (for the
	scroll bar and such)."
	desiredWidth := longest + (self bounds width - self
innerBounds width) + 10.

	"Do I need to do both of these? I think	I do,
	because there are some times when #width: 
	doesn't do it, but it seems silly."
	self minWidth: desiredWidth.
	self width: desiredWidth.

I'd like to get rid of it. Is this functionality
already present somewhere in Squeak? And if it's not,
how can I make it so that the #resizeToFit method is
called whenever someone types into the text box (or at
least whenever someone hits Ctrl-Something inside the
text box)? I've tried overriding #keyStroke: or
#setText:, but neither one of those works. (I
definitely don't understand Morphic yet. :)

Adam Spitz

Post your free ad now!

More information about the Squeak-dev mailing list