Quantcast
Viewing all articles
Browse latest Browse all 10

Using Runtime CSS to Change embedded True Type fonts at Runtime in Flex Applications

This is installment 3 in the series.
The previous 2 related posts are:

Changing embedded True Type fonts at Runtime in Flex Applications

Using Modules to Change embedded True Type fonts at Runtime in Flex Applications

This approach is a little different.
The application is very similar, but instead of using a loader or module loading, I use the StyleManager to load a runtime CSS swf (which was compiled from a CSS file).

Here is one of the style sheets that is tuned into a CSS swf:

  1.  
  2. /* CSS file */
  3. @font-face {
  4.     src:url("assets/arial.ttf");
  5.     fontFamily: myFont;
  6. }
  7.  
  8. @font-face {
  9.     /* Note the different filename for boldface. */
  10.     src:url("assets/arialbd.ttf");
  11.     fontFamily: myFont; /* Notice that this is the same alias. */
  12.     fontWeight: bold;
  13. }
  14.  
  15. @font-face {
  16.     /* Note the different filename for italic face. */
  17.     src:url("assets/ariali.ttf");
  18.     fontFamily: myFont; /* Notice that this is the same alias. */
  19.     fontStyle: italic;
  20. }
  21.  
  22. @font-face {
  23.         /* Note the different filename for bold-italic face. */
  24.         src:url("assets/arialbi.ttf");
  25.         fontFamily: myFont; /* Notice that this is the same alias. */
  26.         fontWeight: bold;
  27.         fontStyle: italic;
  28. }
  29.  
  30. .myPlainStyle {
  31.         fontSize: 11;
  32.         fontFamily: myFont;
  33.  }
  34.  
  35. .myBoldStyle {
  36.         fontSize: 11;
  37.         fontFamily: myFont;
  38.         fontWeight: bold;
  39. }
  40.  
  41. .myItalicStyle {
  42.         fontSize: 11;
  43.         fontFamily: myFont;
  44.         fontStyle: italic;
  45. }
  46.  
  47. .myBoldItalicStyle {
  48.         fontSize: 11;
  49.         fontFamily: myFont;
  50.         fontWeight: bold;
  51.         fontStyle: italic;
  52. }
  53.  

Here is the application.
Most of this should look familiar, except for the new stylesheet loading code.

  1.  
  2. <?xml version="1.0"?>
  3. <!– styles/runtime/BasicApp.mxml –>
  4. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="loadRuntimeStyleSheet(‘Sans’);">
  5. <mx:Panel height="100%" width="100%">
  6. <mx:TextArea id="input" height="100%" width="100%" fontSize="{fontSize}" />
  7. <mx:ControlBar>
  8.         <mx:VBox>
  9.                 <mx:HBox>
  10.                         <mx:Button label="_sans" click="unloadStyleSheet(fontName);loadRuntimeStyleSheet(‘Sans’)"/>
  11.                         <mx:Button label="Arial" click="unloadStyleSheet(fontName);loadRuntimeStyleSheet(‘Arial’)" />
  12.                         <mx:Button label="Trebuchet" click="unloadStyleSheet(fontName);loadRuntimeStyleSheet(‘Trebuchet’)" />
  13.                         <mx:Button label="Rotate +1" click="{++input.rotation}" />
  14.                         <mx:Button label="Rotate -1" click="{–input.rotation}" />
  15.                         <mx:Button label="Small" click="fontSize=6" />
  16.                         <mx:Button label="Medium" click="fontSize=11" />
  17.                         <mx:Button label="Large" click="fontSize=20" />
  18.                         <mx:Button label="Normal" click="applyStyle(‘myPlainStyle’);this.fontWeight=’normal’;this.fontStyle=’nornmal’"/>
  19.                         <mx:Button label="Bold" click="applyStyle(‘myBoldStyle’);this.fontWeight=’bold’;this.fontStyle=’nornmal’"/>
  20.                         <mx:Button label="Italic" click="applyStyle(‘myItalicStyle’);this.fontWeight=’normal’;this.fontStyle=’italic’"/>
  21.                         <mx:Button label="Bold Italic" click="applyStyle(‘myBoldItalicStyle’);this.fontWeight=’bold’;this.fontStyle=’italic’"/>
  22.                 </mx:HBox>
  23.  
  24.                 <mx:HBox>
  25.                         <mx:Label text="current StyleSheet: {currentStyleSheet}"/>
  26.                         <mx:Label text="fontName: {fontName}"/>
  27.                         <mx:Label text="fontSize: {fontSize}"/>
  28.                         <mx:Label text="fontWeight: {fontWeight}"/>
  29.                         <mx:Label text="fontStyle: {fontStyle}"/>
  30.                 </mx:HBox>
  31.  
  32.                 <mx:HBox>
  33.                         <mx:Label id="l2" text="{progMessage}"/>
  34.                         <mx:Label id="l1" text="{progBar}"/>
  35.                 </mx:HBox>
  36.         </mx:VBox>
  37. </mx:ControlBar>
  38. </mx:Panel>
  39.  
  40. <mx:Script>
  41.     <![CDATA[
  42.     import mx.styles.StyleManager;
  43.     import mx.events.StyleEvent;
  44.     import mx.controls.Alert;
  45.  
  46.         [Bindable]
  47.     public var fontName:String = "_sans";
  48.         [Bindable]
  49.     public var fontSize:int = 11;
  50.         [Bindable]
  51.     public var fontWeight:String="normal";
  52.         [Bindable]
  53.     public var fontStyle:String="normal";
  54.     [Bindable]
  55.     public var currentStyleSheet:String="_sans";
  56.  
  57.         [Bindable]
  58.     public var progBar:String = "";
  59.     [Bindable]
  60.     public var progMessage:String = "";
  61.  
  62.  
  63.     public function loadRuntimeStyleSheet(fontName:String):void {
  64.         progBar = "";
  65.         progMessage = "";
  66.  
  67.         this.fontName=fontName;
  68.         var StyleSheet:String=fontName+".swf";
  69.         currentStyleSheet=StyleSheet;
  70.  
  71.         var myEvent:IEventDispatcher =
  72.             StyleManager.loadStyleDeclarations(StyleSheet,true);
  73.         myEvent.addEventListener(StyleEvent.COMPLETE, loadRuntimeStyleSheetComplete);
  74.         myEvent.addEventListener(StyleEvent.PROGRESS, progressEventHandler);
  75.         myEvent.addEventListener(StyleEvent.ERROR, onError);
  76.     }
  77.  
  78.     public function loadRuntimeStyleSheetComplete(e:StyleEvent):void {
  79.                 applyStyle(‘myPlainStyle’);
  80.                 input.setStyle( "fontFamily", ‘myFont’ );
  81.                 if(input.text.length==0){
  82.                         setText();
  83.                 }
  84.                 if(this.fontName=="Sans"){
  85.                         input.setStyle("fontFamily","_sans");
  86.                 }
  87.     }
  88.  
  89.     public function applyStyle(ss:String):void {
  90.                 input.styleName=ss;
  91.     }
  92.  
  93.     public function unloadStyleSheet(ss:String):void {
  94.                 StyleManager.unloadStyleDeclarations(
  95.                     ss, true);
  96.     }
  97.  
  98.     private function progressEventHandler(e Image may be NSFW.
    Clik here to view.
    :P
    rogressEvent):void {
  99.         progBar += ".";
  100.         progMessage = "StyleSheet " +  Math.round((e.bytesLoaded/e.bytesTotal) * 100) + "% loaded";
  101.     }
  102.  
  103.     public function onError(e:StyleEvent):void
  104.     {
  105.                 fontNotLoaded();
  106.     }
  107.  
  108.     private function fontNotLoaded():void{
  109.         Alert.show(‘font not loaded! Defaulting to _sans’);
  110.                 progBar = "";
  111.         progMessage = "Module NOT Loaded!";
  112.         this.fontName="_sans";
  113.         this.fontWeight=‘normal’;
  114.         this.fontStyle=‘normal’;
  115.         setText();
  116.         progMessage = "";
  117.     }
  118.  
  119.     public function setText():void {
  120.         input.text="This is the theme to Garry’s Show,\n"
  121.                                 +"The theme to Garry’s show.\n"
  122.                                 +"Garry called me up and asked if I would right his theme song.\n"
  123.                                 +"I’m almost halfway finished,\n"
  124.                                 +"How do you like it so far,\n"
  125.                                 +"How do you like the theme to Garry’s Show.\n\n"
  126.                                 +"This is the theme to Garry’s Show,\n"
  127.                                 +"The opening theme to Garry’s show.\n"
  128.                                 +"This is the music that you hear as you watch the credits.\n"
  129.                                 +"We’re almost to the part of where I start to whistle.\n"
  130.                                 +"Then we’ll watch ‘It’s Garry Shandling’s Show’.\n\n"
  131.                                 +"This was the theme to Garry Shandling’s show.";
  132.     }
  133.  
  134.     ]]>
  135. </mx:Script>
  136. </mx:Application>
  137.  

A zip file of an entire Flex Builder 2.0.1 project with this code can be downloaded here.

I hope some of you find this demo useful.

-Kyle


Viewing all articles
Browse latest Browse all 10

Trending Articles