繁体   English   中英

Flex中文字的高度

[英]Height of Text in Flex

如何获得从ActionScript动态创建的Text组件的高度。 例如,如果您有以下内容:

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

如何获得温度的高度?

您可以调用validateNow()以确保应用了样式(以及相关的高度)

迹(temp.height);

根据评论进行编辑:

好的,我知道为什么了,因为您依赖默认高度,所以Control直到UI绘制它之前都没有height属性,因此您必须在将其添加到父对象后才能返回它。 因此,当您单击文本时,此简单应用将返回22:

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

但这仅是因为直到绘制完该项目后,我才尝试获取高度。

或者建立user294702所说的内容:这也可以。

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

希望您今天喜欢UI课,我不能接受提示,但请给我积极的评价或建设性的批评。 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM