Jos*_*hua 21 apache-flex air flash actionscript-3
我正在尝试根据它将包含的文本来计算WIDE如何制作我的按钮,当我尝试谷歌如何计算与某些文本的宽度一样简单的东西时,我会试着去试图通过明显无意义的深奥反直觉伏都教.那里的任何人都可以帮我简化我如何编写这样的函数:
public function HowWideWouldThisTextBeIfItWereInThisButton(Text:String,Container:Button):int {
...
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
quo*_*uoo 21
只要您在UIComponent中,就可以使用measureText函数.
public function howWideWouldThisTextBeIfItWereInThisButton(text:String,container:Button):int {
var lineMetrics:TextLineMetrics = container.measureText(text);
return lineMetrics.width;
}
Run Code Online (Sandbox Code Playgroud)
话虽这么说,如果你没有设置宽度,flex按钮组件应自动调整大小到文本的宽度.这样,如果你需要文本宽度,你可以调用textWidth属性.
这适用于任何格式,大小,字体类型.不要忘记属性"autoSize"和"wordWrap"!
var tf:TextField = new TextField();
addChild(tf);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.wordWrap = false;
tf.text = "Whatever here";
tf.width = tf.textWidth + 4; // add 2 pixels-gutters left & right
Run Code Online (Sandbox Code Playgroud)
你的按钮需要"tf.width"宽...