You would think it would be easy to set a TextArea’s height to its contentHeight. Well, it is easy once you dig, and dig and dig for properties. Why there isn’t a property setHeightToContentHeight is beyond me. But there’s also not a property fixBugsOnApplicationInit which I think would be pretty darn handy.
This seems to be working for me:
<s:TextArea
id="ta"
updateComplete="{ ta.height = ta.scroller.viewport.contentHeight + 2 }"; />
“Why is there the +2 at the end?”, you ask.
“I didn’t build the frakking thing!”, I reply.
Really, I have no clue why I have to tack on 2 to the height but if I don’t then sometimes I get the dreaded vertical scrollBar. So, I add 2. Call me crazy.
Instructions
Just add text to the text input TextArea and it will be copied to the text output field. The height of the text output TextArea will grow as the number of lines of text increase.
| view source |
You might want to set the verticalScrollPolicy=”off” of the TextArea. Otherwise the scrollBar will blink in and out when content is added. I left it on for the example so that you could see the effect.





You always slove my technical problems, thanks a lot, most of the things you post are the long time unsolved querries of my mind… thanks for ya sharing.
@John, glad to be of service.
whenever i encounter a +2 in flex layout, i always think "borderThickness".
possibly, the textArea's height is 2 bigger than the scrollbar's height because the scrollbar has a border around it, and it is inset inside the textArea. (?)
thanks for this solution. hopefully it will work better for me than to count all the lines and multiply them by the line height and add a magic number.
(do you know any reason why textArea behaves differently in AIR than Flex?)
@Jeff I hate it when I have to add some magic/arbitrary number to fix some layout. But we do what we must. Yeah, no idea why the TextArea would be different in Flex than AIR.
Thank you very much!
Thanks for TextArea solution. I think the 2px issue has to do with the gutter around all Text Display Objects. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextLineMetrics.html