Kesandal
Kesandal

Reputation: 1303

Change font-color of TextField inside MovieClip

I've a problem to change the font-color of a TextField after it has been added as a child to a MovieClip.

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 can I Change the font-color after the last line?

Upvotes: 4

Views: 23434

Answers (3)

Jamie Ciocco
Jamie Ciocco

Reputation: 123

You can also just use the textColor property of a textfield:

capt.textColor = 0xFF0000;

Upvotes: 2

Josh
Josh

Reputation: 8149

Assuming the TextField falls out of scope after that last line (you don't show enough to know if it does or not), you'll need to loop 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;
    }
}

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

Upvotes: 4

David Mear
David Mear

Reputation: 2254

It sounds like you're looking for TextField.setTextFormat(). You can either adjust your original TextFormat or just make a whole new one.

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

Upvotes: 4

Related Questions