3 Replies Latest reply on May 11, 2011 9:39 AM by Flex harUI

    How To register fonts at runtime in flex 4 ?

    Mohit Verma1234

      Hi Guys

       

      I stuck in a problem .Actually i want to register fonts at runtime here is sample of code i am using :

       

       

      private function loadFont(url:String):void{

      var loader:Loader = new Loader();

      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontLoaded);

       

      loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);        

                  loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

                  loader.addEventListener(IOErrorEvent.DISK_ERROR, ioDiskHandler);

                  loader.addEventListener(IOErrorEvent.NETWORK_ERROR, ioNetworkHandler);

                  loader.addEventListener(IOErrorEvent.VERIFY_ERROR, ioVerifyHandler);

                 

                  loader.load(new URLRequest(url));

      }

      //===============================================================================

      // funtion fontLoaded :: called after font loaded by loadewr class

      private function fontLoaded(evt:Event):void

      {

      var fontLib:Class

      //trace("loaded " + "<<>> "+fontCounter);

      //FlexGlobals.topLevelApplication.fontArr[fontCounter];

      if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Arial") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontArial") as Class;

      //Font.registerFont(fontLib.Arial_2);

      Font.registerFont(fontLib._Arial);

      Font.registerFont(fontLib._Arial_bold);

      Font.registerFont(fontLib._Arial_italic);

      Font.registerFont(fontLib._Arial_bolditalic);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "A Little Pot") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontAlittlePot") as Class;

      Font.registerFont(fontLib._Alittlepot);

      Font.enumerateFonts();


      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Book Antiqua") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontBookAntiqua") as Class;

      //Font.registerFont(fontLib.Arial_2);

      Font.registerFont(fontLib._BookAntiqua);

      Font.registerFont(fontLib._BookAntiqua_bold);

      Font.registerFont(fontLib._BookAntiqua_italic);

      Font.registerFont(fontLib._BookAntiqua_bolditalic);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Fine Hand LET") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontFineHand") as Class;

      Font.registerFont(fontLib._FineHand);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "NewBskvll BT") {

      fontLib =  evt.target.applicationDomain.getDefinitionByName("fontNewBaskerVille") as Class;

      Font.registerFont(fontLib._NewBaskervilleBt);

      Font.registerFont(fontLib._NewBaskervilleBt_bold);

      Font.registerFont(fontLib._NewBaskervilleBt_italic);

      Font.registerFont(fontLib._NewBaskervilleBt_bolditalic);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Segoe Print") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontSegoePrint") as Class;

      Font.registerFont(fontLib._SegoePrint);

      Font.registerFont(fontLib._SegoePrint_bold);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "SF Burlington Script SC") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontSFBurlingtonScriptSC") as Class;

      Font.registerFont(fontLib._SFBurlingtonScriptSC);

      Font.registerFont(fontLib._SFBurlingtonScriptSC_bold);

      Font.registerFont(fontLib._SFBurlingtonScriptSC_italic);

      Font.registerFont(fontLib._SFBurlingtonScriptSC_bolditalic);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Pica") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontPica") as Class;

      Font.registerFont(fontLib._Pica);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Signature") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontSignature") as Class;

      Font.registerFont(fontLib._Signature);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Verdana") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontVerdana") as Class;

      Font.registerFont(fontLib._Verdana);

      Font.registerFont(fontLib._Verdana_bold);

      Font.registerFont(fontLib._Verdana_italic);

      Font.registerFont(fontLib._Verdana_bolditalic);

      Font.enumerateFonts();

      } else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "SFBurlingtonScript") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontSFBurlingtonScript") as Class;

      Font.registerFont(fontLib._SFBurlingtonScript);

      Font.registerFont(fontLib._SFBurlingtonScript_bold);

      Font.registerFont(fontLib._SFBurlingtonScript_italic);

      Font.registerFont(fontLib._SFBurlingtonScript_bolditalic);

      Font.enumerateFonts();

      }

      else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Paper Cuts") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontPapercuts") as Class;

      Font.registerFont(fontLib._Papercuts);

      Font.enumerateFonts();

      }

      else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Sansation") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontSansation") as Class;

      Font.registerFont(fontLib._Sansation);

      Font.registerFont(fontLib._Sansation_bold);

      Font.enumerateFonts();

      }

      else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Times New Roman") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontTimes") as Class;

      Font.registerFont(fontLib._Times);

      Font.registerFont(fontLib._Times_bold);

      Font.registerFont(fontLib._Times_italic);

      Font.registerFont(fontLib._Times_bolditalic);

      Font.enumerateFonts();

      }

      else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "Handserif") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontHanSrf") as Class;

      Font.registerFont(fontLib._Handserif);

      Font.enumerateFonts();

      }

      else if(FlexGlobals.topLevelApplication.fontArr[fontCounter].label == "WC RoughTrad") {

      fontLib =  evt.target.applicationDomain.getDefinition("fontWCRoughTrad") as Class;

      Font.registerFont(fontLib._WCRoughTrad);

      Font.enumerateFonts();

      }


      //var xmlNode:XML = new XML("<font name='"+ FlexGlobals.topLevelApplication.fontArr[fontCounter].label +"' load='true'/>")

      //FlexGlobals.topLevelApplication.fontLoadStatusXML.appendChild(xmlNode);


      if(fontCounter < totalFonts-1) {

      fontCounter++;

      //FlexGlobals.topLevelApplication.taSetText.text += "\nloading "+ FlexGlobals.topLevelApplication.fontArr[fontCounter].label;

      this.loadFont(FlexGlobals.topLevelApplication.fontArr[fontCounter].path);

      }

      }

      It is working in flex 3 but not working in flex 4 .. Need help ..
      Regards
      Mohit