计算文本宽度在ActionScript和Flex宽度、文本、Flex、ActionScript

由网友(山炮。进城勒)分享简介:我试图计算出有多宽,使我的按钮,根据它包含文本,当我试图谷歌如何calcuate的东西简单化的一些文本的宽度,我去斗眼只是试图涉水通过显然是荒谬的深奥反直觉的巫术。有任何人可以帮助简化对我,我怎么会写这样的功能:I'm trying to calculate how WIDE to make my button, b...

我试图计算出有多宽,使我的按钮,根据它包含文本,当我试图谷歌如何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,您可以使用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.

阅读全文

相关推荐

最新文章