开发者

Height of Text in Flex

开发者 https://www.devze.com 2022-12-26 05:32 出处:网络
How can you get the height of the Text component that\'s been created dynamically from ActionScript. For instance, if you have something like:

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 Tex开发者_如何学编程t;
temp.width = 50;
temp.text = "Simple text";

how to get height of temp?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号