Actionscript – Converting string to uint in actionscript / Flex

actionscriptactionscript-3apache-flex

I am creating a component and want to expose a color property as many flex controls do, lets say I have simple component like this, lets call it foo_label:


<mx:Canvas>
    <mx:Script>
        [Bindable] public var color:uint;
    </mx:Script>
    <mx:Label text="foobar" color="{color}" />
</mx:Canvas>

and then add the component in another mxml file, something along the lines of:


<foo:foo_label color="red" />

When I compile the compiler complains: cannot parse value of type uint from text 'red'. However if I use a plain label I can do

<mx:Label text="foobar" color="red">

without any problems, and the color property is still type uint.

My question is how can I expose a public property so that I can control the color of my components text? Why can I use the string "red" as a uint field for the mx controls but cannot seem to do the same in a custom component, do I need to do something special?

Thanks.

Best Answer

Color is not a property, it is a style. You need to define the style like this:

[Style(name="labelColor", type="uint", format="Color" )]

(enclose it in tag if you define it directly in MXML). You then need to add some ActionScript to handle this style and apply it to whichever control you need, please refer to http://livedocs.adobe.com/flex/3/html/help.html?content=skinstyle_1.html for more information.

Related Topic