Change font color of TextField inside MovieClip

I have a problem changing the font color of a TextField after adding it to the MovieClip as a child.

My code:

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);

// .....

      

How do I change the font color after the last line?

+3


source to share


3 answers


Assuming you're TextField

out of scope after that last line (you're not showing enough to know what it does or not), you need to go through the button to get the TextField and do it from there.

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;
    }
}

      



This assumes there is only one TextField, of course. If there are multiple, I would increase the MovieClip and add a public property for the value you want to change.

+4


source


It looks like you are looking for TextField.setTextFormat () . You can customize the original one TextFormat

or just create a completely new one.



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

      

+3


source


You can also just use the textColor property of the textbox:

capt.textColor = 0xFF0000;

      

+1


source







All Articles