Automatically resize Text/TextArea based on content (autoSize) in Flex
by Nicolas Noben
UPDATE: turns out that doesn’t always work. the autoSize property is not reliable at all…
This, should do!
var ta_height:uint = 25;
field.validateNow();
for(var i:int=0; i < field .mx_internal::getTextField().numLines; i++) {
ta_height += field.mx_internal::getTextField().getLineMetrics(i).height;
}
derivedHeight = ta_height;
Thanks Vaan.
Original post:
Adobe’s dodgy textHeight sure doesn’t do the trick. I end up getting a textfield of 2000px height while it clearly looks like 300 tops.
This, however, works.
the code
private function resizeMe(field:TextArea) :void
{
field.validateNow();
field.mx_internal::getTextField().autoSize = TextFieldAutoSize.LEFT;
field.height = field.mx_internal::getTextField().height;
}
Just use that on your TextArea or Text component:
creationComplete="resizeMe(this.myTextAreaInstance)"
Thanks to Vaan for some insight about the mx_internal::getTextField().
Related Posts
- Easy Form Validation and Submit Button enable-disable in Flex 3
- Capture CTRL+N / CMD+N in Flex/Air/AS3
- FlexBook, the must have component for flex
- Adobe launches the Adobe AIRâ„¢ Market Place
- Class to Open object(s) from file with Flex in Air

Stay focussed
Del.icio.us
Reddit
Stumble it
Digg it


ian fltiman Says:
December 1st, 2008 at 10:57 pmThanks for this workaround for a function (textHeight) that Adobe can’t get right. I found it was misreporting the height in my textArea too, which was for me useless. This hack now makes it work.
However, I have read that using mx_internal means that your code might not work with future releases of Flex as Adobe uses it to
mark things that may change in future versions of the framework so you use it at your peril. Does that go for this hack then? I really don’t want to spend ages defining a component that will fall over in the future especially as resizing the text area is absolutely key to what I want to do.
Nicolas Noben Says:
December 2nd, 2008 at 8:46 amHey Ian, the fact that they haven’t fixed that bug since forever is enough reassurance that it won’t change any time soon.
I mean, do that component anyway, it won’t take you a lifetime
Johan Munkestam Says:
December 3rd, 2008 at 6:59 pmI had the same problem (and written a couple of posts about it) but I found one way that for me works at all times now, and isn’t as much of a hack as…well, maybe you can read it and give me your feedback as to what you think of my approach: http://thoughts.novaleaf.com/johanm/2008/12/03/flex-solution-to-automatic-re-sizing-of-textarea-not-working-at-run-time-even-after-using-invalidatenow/
(Read the previous post for more digging into my angle of the issue and the references I used, including your posting.)
kevin Says:
December 24th, 2008 at 10:21 amThanks Nicolas!
Works perfectly.
kevin Says:
December 24th, 2008 at 3:08 pmupdate: this stopped working for me when I had more than a few lines of text. But simply adding a +1 seems to do to the trick:
field.height = field.mx_internal::getTextField().height + 1;
dan Says:
January 10th, 2009 at 4:41 amThank you very much. This saved me a ton of time!
Mani Says:
March 6th, 2009 at 5:24 pmHi Dude….
worked Perfect… you saved my day
Chris Wyatt Says:
March 12th, 2009 at 6:55 amJust wanted to add something that might help others. I made a script to dynamically add loads of text from XML as seperate TextAreas and Labels etc. Trouble was the number of lines was being misreported by Flash. Turned out that using a specific width rather than a percentage for each TextArea did the trick, took me ages to figure it out.
All is working fine now
rossi123 Says:
March 29th, 2009 at 2:28 amThanks a lot for that!
My problem was similar: Many textAreas in a vBox-container not willing to talk about their height nor width. Even when assigning a fixed value for height and width the textWidth and textHeight tells a wrong value.