Run time CSS loading guide
Posted by Almost Noted on May 16, 2007
Why loading at runtime?
By loading style sheets at run time, you can load images (for graphical skins), fonts, type and class selectors, and programmatic skins into your Flex application without embedding them at compile time. This lets skins and fonts be partitioned into separate SWF files, away from the main application. As a result, the application’s SWF file size is smaller, which reduces the initial download time. However, the first time a run-time style sheet is used, it takes longer for the styles and skins to be applied because Flex must download the necessary CSS-based SWF file.
Three steps process to runtime CSS Style Sheets loading
- Write a CSS file for your application.
- Compile the CSS file in a SWF file by using the mxmlc compiler or right click on the css file in Flex Builder and select compile CSS to SWF. You can locate the swf file in the bin directory
- Call the StyleManager.loadStyleDeclarations() method in your Flex application. This method loads the CSS-based SWF file into your application. When this method executes, Flex loads the new CSSStyleDeclarations into the StyleManager.
Insert the below code into the <mx:Application/> tag
initialize = “StyleManager.loadStyleDeclarations( ‘style1CSS.swf’ )“
<mx:Application
xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute”
initialize=”StyleManager.loadStyleDeclarations( ‘style1CSS.swf’ )”
viewSourceURL=”srcview/index.html”>
Let me know if you have any clarification.
Sweta said
Hi
If i want to compile css file at run time, then what should do?
Andrés said
You can’t with felx alone, you need to create the CSS file and send it to the flex compiler, then it will create a swf with all the style declarations for you to load.
Tom Bonner said
Thanks for posting this — it solved a problem I had figuring out why my stylesheet.swf wasn’t loading. However, I think there is a type in your code.
My compiler objected to a missing double quote mark — there should be a double quote after the initialize = statement and before the StyleManager… text.
In addition, on my laptop, the single quotes around the style1CSS.swf object and the double quote at the end of the statement are typesetter’s quotes — when I copied and pasted the statement, Flexbuilder3 ignored the quote marks. When I changed the marks to standard ASCI characters, everything worked fine.
flexrays said
Hi Tom..
Thanks a lot for commenting on my post. I have corrected it.
Hi Andres…
As per the point 2 in my post, I am adding the CSS file to the flex compiler. Can you please elaborate your question. Thanks
Redni said
Very good article explaining runtime dynamic stylesheet implementation on all flex components
http://askmeflash.com///article_m.php?p=article&id=6
Redni said
use stylemanager class to change dynamic stylesheet or theme change in flex ,
You can do that by using setstyle and getstyle methods on runtime .
More details are on this article
http://askmeflash.com/article_m.php?p=article&id=6