views:

235

answers:

2

How can you get the height of the Text component that's been created dynamically from ActionScript. For instance, if you have something like:

var temp:Text = new Text;
temp.width = 50;
temp.text = "Simple text";

how to get height of temp?

+1  A: 

trace(temp.height);

Edit Based on Comments:

OK I see why, because you are relying on a default height, the Control does not have a height property until the UI draws it so you wouldn't be able to return it until after it's added to the parent object. so this simple app will return 22 when you click on the text:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
 <mx:Script>
  <![CDATA[
   import mx.controls.TextInput;
   private function init():void{
    var bar:TextInput = new TextInput
    bar.x = 5;
    bar.y = 5
    bar.width = 50;
    bar.name = "bar";
    foo.addChild(bar);
    bar.addEventListener(FlexEvent.CREATION_COMPLETE,runthis);
   }

   private function runthis(evt:FlexEvent):void{
    trace(TextInput(evt.currentTarget).height);
   }
  ]]>
 </mx:Script>
 <mx:Canvas x="10" y="10" width="200" height="200"  id="foo">
 </mx:Canvas>

</mx:Application>

But that's only because I'm not trying to get the height until well after the item has been drawn.

OR building off what user294702 said: This works too.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
 <mx:Script>
  <![CDATA[
   import mx.events.FlexEvent;
   import mx.controls.TextInput;
   private function init():void{
    var bar:TextInput = new TextInput
    bar.x = 5;
    bar.y = 5
    bar.width = 50;
    bar.name = "bar";
    foo.addChild(bar);
    foo.validateNow();
    trace(bar.height);
   }
  ]]>
 </mx:Script>
 <mx:Canvas x="10" y="10" width="200" height="200"  id="foo">
 </mx:Canvas>

</mx:Application>

I hope you've enjoyed your UI lesson today, I can't accept tips but please give positive results on my evaluation or constructive criticism. :-)

invertedSpear
Actually, this is the first thing I've tried, but I keep getting 0. That's the strangest thing. Width is explicitly set, and after this code snippet it has proper value - 50. But height is 0 no matter what I do. I have also tried temp.measuredHeight and temp.getExplicitOrMeasuredHeight() but they return 0 as well.
kevin
Oh OK, I see. Do you have any idea (or actually do you know is it even possible) how to calculate height before control is drawn? What I need is to calculate cumulative height of several Text components. Based on that, I'll know much of them I need to draw. Is it possible to measure the component before it is rendered?
kevin
Actually, I guess that I could use measureText() function for this purpose, but I was kind of hoping that height could be obtained from directly from component.
kevin
measure text is misleading anyway. the text control is slightly bigger than the text inside it. As user294702 said a validatenow() should force the control into the UI to be measured. Here's something else you can do though. Break your function into two. You can change the click event to a creation complete event (which I changed in my example) and continue what you are doing in that event handler.
invertedSpear
Thanks both ivertedSpear and user294702. I have done something similar using creationComplete event handler.
kevin
+2  A: 

You can call validateNow() to make sure the style is applied (and the height relevant)

+1 cause I didn't think of sharing that first :)
invertedSpear