Showing posts with label ActionScript. Show all posts
Showing posts with label ActionScript. Show all posts

Monday, June 22, 2009

Default stylesheet in an SWC (Flex Library Project), and embedded font rotation

For ages now I've been trying to figure out how I can use a StyleSheet from inside my Flex Library Project in ActionScript. I kept reading that it is very resource intensive to be calling UIComponent.setStyle(...) at runtime, so I wanted to set all my styles using a StyleSheet. The LiveDocs on the subject seem to say that there are two ways to load a StyleSheet:
  1. From inside MXML in your Application using the <Style source="assets/styles.css"/> tag
  2. By loading an external stylesheet SWF file (compiled from a CSS file) using the
    StyleManager. loadStyleDeclarations(url)

Then I finally found the solution in this article (right near the bottom of the page in the comment by Henk). And that pointed me to this LiveDoc - Applying styles to your custom component which solved the problem. Scroll most of the way down until you get to the part called "Applying styles from a defaults.css file".

If you don't want to read the articles above, here is my brief summary.

Using a StyleSheet in your FlexLibrary Project:
  1. create a StyleSheet in the src directory of your project, and call it defaults.css
  2. open the project Properties > Flex Library Build Path and check the src/defaults.css file under the Assets tab
  3. define your styles inside the defaults.css StyleSheet
Restrictions:
  • You can include only a single style sheet in the SWC file
  • The file must be named defaults.css
  • The file must be in the top-most directory of the SWC file
*Note: it appears that having a defaults.css file in your Flex Application project (swf) also works without having to specify the stylesheet in the mxml (e.g. <mx:Style source="defaults.css"/> is not needed).


Using Embedded Fonts inside Flex Library Project/SWC:
If you want to use an embedded font inside your library project you have two ways of doing this:
  1. Embed the font inside an ActionScript file like this (the font is inside the project src/assets directory):
    [Embed(source='/assets/verdana.ttf'fontName='localVerdana'
           mimeType='application/x-font')]
    public var verdanaFont:Class;
    ...
    setStyle("fontFamily""localVerdana");
  2. Or put your embedded ttf font in your project's src directory and embed the fond using the defaults.css file (the font must be in the src directory for this to work - see this Adobe Bug for more details):
    defaults.css:
    @font-face {
      font-family: localVerdana;
      font-weight: normal;
      src: url("verdana.ttf");
    }
    .label {
      /* must use embedded font for label rotation to work */
      font-family: localVerdana;
      /* must specifically set the font weight */
      font-weight: normal;
    }
Note that if you want to set the DisplayObject.rotation property then you have to use an embedded font otherwise it doesn't work.

Here is a simple example of font/label rotation (right click "View Source" for the code).
In this example I actually included three fonts - Verdana plain, Verdana bold, and Comic Sans.

* Note that fonts can greatly increase the size of your SWF. The three fonts that I used above are all about 150 KB each. One way to reduce the size of your SWF is to restrict the unicode character range (meaning that only part of the font set is included in your SWF). Here is the LiveDoc on Using Fonts and Setting Character Ranges.

Ben Stucki's blog helped me figure out how to embed a bold and plain font in CSS.