Dynamic height of TextArea (Flex and AIR)

7 sec read

Believe it or not, this is actually not trivial. Instead of getting text height from TextArea.textHeight, retrieving from TextArea.textField.textHeight works. Also note validateNow() before TextArea.textField.textHeight.

<?xml version="1.0" encoding="utf-8"?>
<mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml"
	creationComplete="init();" 
	horizontalScrollPolicy="off" verticalScrollPolicy="off" >
<mx:Script>
	<![CDATA[
		
		private function init():void
		{
			this.height = this.textField.textHeight + this.textField.getLineMetrics(0).height;			
		}
		override public function set text(val:String):void
        {
        		
            textField.text = val;
            validateNow();

            //set height to the height of the text + 1 line
            height = textField.textHeight +  textField.getLineMetrics(0).height;

        }
		
	]]>
</mx:Script>
	
</mx:TextArea>



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

How much money did I make from an app?

Undoubtedly some people are very successful in making money by developing a smartphone app. Back in 2012 I developed an app called “Handbook of Brain” which is a collected resources of brain anatomy, function and diseases. I put the app i
Xu Cui
27 sec read

Handy programs to visualize NIRS data (2): plotTopoMap

Often you need to view the spatial pattern of activation as in the example below. plotTopoMap allows you to do that. It probably only works for Hitachi devices where the spatial relationship between channels are known. In the above example, the activ
Xu Cui
37 sec read

Flash 3D video demo

Racer Ostrova Zombie
Xu Cui
0 sec read

8 Replies to “Dynamic height of TextArea (Flex and AIR)”

  1. I think you don’t really need to override the set text function, you can just write validateNow() in init() before adjusting the height

  2. You dont really need to override the function. To get the text height of any textarea all u need to do is
    textArea.getTextField().textHeight

    For all you guys working on textArea, getTextField() is god sent!!!
    You can do anything and everything using getTextField()

  3. Hey, I m getting following error on line 15 above.
    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    I debugged and found out that the textField is not instantiated. I used this.mx_internal::getTextField() to get the text field also but to avail no success.
    Any hints..?

  4. Dude, it’s years since you posted this but after 5 hours of bashing my head against the wall, this solution is a life saver. Huge thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *