我们怎样才能得到Spark TextInput像素的实际文字宽度?

时间:2010-12-21 10:32:45

标签: flex flash textinput

因为textWidth属性在spark textinput中无法访问,我们如何才能获得该属性?

1 个答案:

答案 0 :(得分:7)

火花中有一种叫TextLineMetrics

的东西

你可以这样做

var tm:TextLineMetrics = label.measureText( name );
var width:int = tm.width;

这是一个例子

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.controls.Text;

        protected function cmdDoAction_clickHandler(event:MouseEvent):void
        {
            var tm:TextLineMetrics = cmdDoAction.measureText(cmdDoAction.label);
            var _width:int = tm.width;

            Alert.show(_width.toString());
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:Button id="cmdDoAction"
          label="Kensodev"
          click="cmdDoAction_clickHandler(event)"/>
    </s:Application>

这将为您提供实际宽度