【问题标题】:Change font-color of TextField inside MovieClip更改 MovieClip 中 TextField 的字体颜色
【发布时间】:2013-01-27 16:44:29
【问题描述】:

在将 TextField 作为子项添加到 MovieClip 后,我无法更改其字体颜色。

我的代码:

var btn:MovieClip = new MovieClip();        
// other code for button

var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.bold = true;
tf.font = "Arial"
tf.color = 0x000000;

var capt:TextField = new TextField();
capt.defaultTextFormat = tf;
capt.width = 200;
capt.height = 50;
capt.text = "Test";
capt.y = 20;
capt.x = 20;
capt.border = false;
capt.selectable = false;    

btn.addChild(capt);

// .....

如何更改最后一行后的字体颜色?

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    假设TextField 在最后一行之后超出范围(您没有显示足够的内容来知道它是否存在),您需要遍历按钮以获取 TextField 并从那里执行.

    var i:uint;
    var l:uint = btn.numChildren; //numChildren and length are 'heavy' getters, never use them as restrictors in a loop
    for ( i = 0; i < l; i++ ) {
        var cur:DisplayObject = btn.getChildAt( i );
        if ( cur is TextField ) {
            ( cur as TextField ).setTextFormat( /*set new TextFormat in here*/);
            break;
        }
    }
    

    当然,这假设只有一个 TextField。如果有多个,我会扩展 MovieClip 并为您要更改的值添加一个公共属性。

    【讨论】:

      【解决方案2】:

      听起来您正在寻找TextField.setTextFormat()。您可以调整原来的TextFormat,也可以制作一个全新的。

      tf.color = 0xFF0000;
      capt.setTextFormat(tf);
      

      【讨论】:

      • 感谢您的回复。是否可以通过 btn 调用“setTextFormat”。 ? btn.capt.setTextFormat(tf) 不起作用。我问的原因是,我正在动态创建按钮。在 btn.addChild(capt) 之后,我将所有按钮保存在一个数组中。
      • 不容易。您最好将 TextFields 也保存到数组中,或者将按钮作为键保存到 Dictionary
      • 实际上这很容易,正如 Apocalyptic0n3 在他们的回答中显示的那样。我以为你有SimpleButtons 出于某种原因。您可以保留对 TextFields 的引用,也可以像这样再次找到它们。
      【解决方案3】:

      您也可以只使用文本字段的 textColor 属性:

      capt.textColor = 0xFF0000;
      

      【讨论】:

        猜你喜欢
        • 2011-01-02
        • 2019-12-24
        • 1970-01-01
        • 2020-11-04
        • 2017-07-14
        • 2019-12-23
        • 2013-11-14
        • 2014-01-19
        • 2015-01-02
        相关资源
        最近更新 更多