计算文本宽度在ActionScript和Flex [英] Calculating Text Width In ActionScript And Flex

查看:118
本文介绍了计算文本宽度在ActionScript和Flex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算出有多宽,使我的按钮,根据它包含文本,当我试图谷歌如何calcuate的东西简单化的一些文本的宽度,我去斗眼只是试图涉水通过显然是荒谬的深奥反直觉的巫术。有任何人可以帮助简化对我,我怎么会写这样的功能:

I'm trying to calculate how WIDE to make my button, based on the text that it will contain, and when I try to google for how to calcuate something as simplistic as the WIDTH OF SOME TEXT, I go cross-eyed just trying to wade through apparently nonsensical esoteric counter-intuitive voodoo. Can anyone out there help simplify for me how I would write a function like this:

public function HowWideWouldThisTextBeIfItWereInThisButton(Text:String,Container:Button):int {
 ...
}

在此先感谢。

Thanks in advance.

推荐答案

只要你在在UIComponent,您可以使用<一个href="http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#measureText%28%29">measureText功能。

So long as you're in a UIComponent, you can use the measureText function.

public function howWideWouldThisTextBeIfItWereInThisButton(text:String,container:Button):int {
   var lineMetrics:TextLineMetrics = container.measureText(text);
   return lineMetrics.width;      
}

话虽这么说,可更换按钮组件会自动调整大小的文本的宽度,如果你没有在其上设置的宽度。这样,如果你需要的文字宽度,你可以调用使用输出textWidth属性。

That being said, the flex button component should automatically size to the width of the text, if you don't set a width on it. That way if you need the text width, you can just call use the textWidth property.

这篇关于计算文本宽度在ActionScript和Flex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆