The StyleManager class in Flex allows you to easily read and write individual style properties from the comfort of your AS3 code.
But it’s not as easy as it could be.
You still have to retrieve each style declaration, make sure they exist, and then either return or set the desired style property. It’s not a lot of work, but it’s repetitive, and we can do better.
Enter the StyleUtil class.
StyleUtil is an all-static class I created with methods to access and modify individual style properties.
There are two basic methods, setCSSStyle() and getCSSStyle(), as well as hex() and hexToString() methods for converting hexadecimal values.
Now, changing the background color of a Canvas container is as simple as calling:
StyleUtil.setCSSStyle ("Canvas", "backgroundColor", StyleUtil.hex (value));
Not too shabby, eh?
Full source code is available here:
http://userflex.wordpress.privatepaste.com/241rTkjoRl
As well as a few sample getters and setters:
http://userflex.wordpress.privatepaste.com/ceKcBegdpA
Update: Code snippets I’ve posted using Private Paste are randomly disappearing, so if the above link no longer exists, here’s a backup.

3 comments
Comments feed for this article
June 24, 2008 at 6:50 am
tamitutor
Hi,
I’ve clicked on your links and they don’t work…Getting an “invalid private paste content id” error. Could you repost?
Thanks,
Tami
June 24, 2008 at 7:22 am
Nick Schneble
Sorry about that! Private Paste deleted a lot of my code snippets even though they were set not to expire.
I’ll try and recreate them as soon as I can!
June 24, 2008 at 10:36 am
Nick Schneble
I’ve updated the post to include a PDF backup of StyleUtil.as (WordPress doesn’t allow most filetypes, including .mxml and .as).
Here’s an example of a getter and setter for the Canvas background color:
public function set canvasBackgroundColor (value : *) : void
{
StyleUtil.setCSSStyle (“Canvas”, “backgroundColor”, StyleUtil.hex (value));
}
public function get canvasBackgroundColor () : uint
{
return StyleUtil.getCSSStyle (“Canvas”, “backgroundColor”);
}