Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 TextFormat在原始AS3中工作,但在Flex中不工作_Actionscript 3_Flash_Apache Flex - Fatal编程技术网

Actionscript 3 TextFormat在原始AS3中工作,但在Flex中不工作

Actionscript 3 TextFormat在原始AS3中工作,但在Flex中不工作,actionscript-3,flash,apache-flex,Actionscript 3,Flash,Apache Flex,这个问题让我抓狂,因为它看起来很简单。我正在尝试将文本格式应用于使用代码创建的文本字段,并在文本更改时将格式保持应用于文本字段。以下代码在raw AS3中按预期工作。我把这些例子分解为尽可能简单的例子 package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; public class Testing extends Sprit

这个问题让我抓狂,因为它看起来很简单。我正在尝试将文本格式应用于使用代码创建的文本字段,并在文本更改时将格式保持应用于文本字段。以下代码在raw AS3中按预期工作。我把这些例子分解为尽可能简单的例子

package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class Testing extends Sprite
    {
        public function Testing()
        {
            var tf:TextField = new TextField();
            addChild(tf);
            tf.border = true;

            var tfor:TextFormat = new TextFormat();
            tfor.align = 'right';
            tfor.size = 30;

            tf.defaultTextFormat = tfor;

            tf.text = 'Testing';

        }
    }
}
但是,Flex中类似的代码的行为方式不同。以下代码导致文本格式不正确

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           creationComplete="_create(event)">
    <fx:Script>
        <![CDATA[
            import mx.core.UITextField;
            import mx.events.FlexEvent;

            protected function _create(event:FlexEvent):void
            {
                var tf:UITextField = new UITextField();
                ui.addChild(tf);
                tf.border = true;

                var tfor:TextFormat = new TextFormat();
                tfor.align = 'right';
                tfor.size = 30;

                tf.defaultTextFormat = tfor;

                tf.text = 'Testing';            
            }

        ]]>
    </fx:Script>

    <mx:UIComponent id="ui" width="100%" height="100%" />

</s:Application>


我意识到我可以使用Flex组件作为文本字段,并以这种方式在其上粘贴格式,但此代码需要与以前编写的代码配合使用。提前感谢您的帮助。

下面的代码可能会对您有所帮助:-不要将其添加到UIComponent,而是将其添加到SpriteVisualElement,它会起作用

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="_create(event)">
    <fx:Script>
        <![CDATA[
            import mx.core.UITextField;
            import mx.events.FlexEvent;

            protected function _create(event:FlexEvent):void
            {
                var tf:UITextField = new UITextField();
                ui.addChild(tf);
                tf.border = true;

                var tfor:TextFormat = new TextFormat();
                tfor.align = 'right';
                tfor.size = 30;

                tf.defaultTextFormat = tfor;

                tf.text = 'Testing';            
            }

        ]]>
    </fx:Script>
    <s:SpriteVisualElement id="ui" width="100%" height="100%" />

</s:Application>


是的,这是正确答案,但需要先设置tf.width才能看到结果。感谢您的回答!有人知道UIComponent缺少什么样的SpriteVisualElement导致了这种情况吗?