Flex 中文本的高度

发布于 2024-08-28 05:48:04 字数 170 浏览 5 评论 0原文

如何获取从 ActionScript 动态创建的 Text 组件的高度。例如,如果您有类似的问题:

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

如何获取温度高度?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

々眼睛长脚气 2024-09-04 05:48:08

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

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

╭ゆ眷念 2024-09-04 05:48:08

跟踪(温度.高度);

根据评论进行编辑:

好的,我明白为什么了,因为您依赖于默认高度,所以在 UI 绘制控件之前,控件没有高度属性,因此您无法返回它,直到添加到父对象后。因此,当您单击文本时,这个简单的应用程序将返回 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 课程,我不能接受提示,但请对我的评估或建设性批评给予积极的结果。 :-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文