How can you change the color of a label at run time in ActionScript 3.0?

I get the following error:

1119: Access of possibly undefined property color through a reference with static
type mx.controls:Label.

The point is that in MXML, color is an attribute of Label. But if I try to say something like:

lblUpgrade.color = "#000000";

he gives this error. I tried to find a job in the last 45 minutes. How can I set this at runtime? Thank!

+5
source share
4 answers

Label does not have a property color, but has a color style that can be set as follows:

lblUpgrade.setStyle("color","#000000");
+10
source

Styles are available as in as3

lblUpgrade.setStyle("color","#000000");
+5
source

- , , setStyle. as3 0x # , , , .

lblUpgrade.setStyle( "color", "0x000000" );

+4

Wow, I fought for 45 minutes AFTER I found this post. I am using Adobe CS6 (don't ask why!), And the only way that finally works for me is this:

/* Create a new TextFormat object, 
which allows you to set multiple text properties at a time. */ 

var tf:TextFormat = new TextFormat(); 
tf.color = 0xFF0000; 

/* Apply this specific text format (red text) to the Label instance. */ 
a_label.setStyle("textFormat", tf);

Hope this helps someone. Source: Adobe Help Center

You can also use TextFormat to change other properties, such as Font, size, etc.

+2
source

All Articles