Technology blog oriented towards good design and impressive web applications.

IdleTogether Home

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





9 Responses


  1. ian fltiman Says:

    Thanks 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.

  2. Nicolas Noben Says:

    Hey 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 :)

  3. Johan Munkestam Says:

    I 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.)

  4. kevin Says:

    Thanks Nicolas!

    Works perfectly.

  5. kevin Says:

    update: 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;

  6. dan Says:

    Thank you very much. This saved me a ton of time!

  7. Mani Says:

    Hi Dude….
    worked Perfect… you saved my day :)

  8. Chris Wyatt Says:

    Just 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 :)

  9. rossi123 Says:

    Thanks 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.

Leave a Comment







  Please note: Comments are reviewed before going public.