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

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

问题描述

我正在尝试根据它将包含的文本计算制作按钮的宽度,并且当我尝试在谷歌上搜索如何计算某些文本的宽度这样简单的内容时,我的眼睛变成了斗鸡眼只是试图通过显然荒谬的深奥的违反直觉的伏都教.任何人都可以帮助我简化我如何编写这样的函数:

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 {
 ...
}

提前致谢.

推荐答案

只要您在 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;      
}

话虽如此,如果您没有在其上设置宽度,则 flex 按钮组件应自动调整为文本的宽度.这样,如果您需要文本宽度,您可以调用使用 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天全站免登陆