Embedding External Fonts explained: method 1

/ Published in: ActionScript 3
Save to your folder(s)

I\'m using Flash CS4.\r\n\r\n1.First thing you need to do is turn your font into a class that can be loaded.\r\n When you create your class pay special attention to the fontName you give it. You will need that later. NOTE: only TrueType fonts will work.\r\n2.Now that your font is a class its accessible to the rest of your code. The difference is, you access it by the fontName you assigned it earlier.\r\n3. WARNING: In-order for your font to show up on the stage, your font has to be embedded in the textField:\r\n4. WARNING: When setting the TextFormat, you MUST use defaultTextFormat. setTextFormat() will not work.\r\n5. SIDE NOTE: If your using an AssetMangement class. (ie. a once stop shop class that loads everything). You provide access to this class and its loaded fonts by creating a variable that is equal to the font in the AssetManagement class. See the example below.

Copy this code and paste it in your HTML
  1. //This line can be in the timeline or if your using a class, within the class, Not within a function.
  3. [Embed(source='/assets/fonts/NeoSans.ttf', fontName="TheCrazyMan", mimeType="application/x-font-truetype")]
  4. public static var FONT_NEOSANS:Class; //<------------Variable must be static in-order for this to work.
  7. //////////////////////////////////////////////////////TextFormat Creation
  9. var format:TextFormat = new TextFormat();
  11. format.font = "TheCrazyMan";
  12. format.color = "0xB10101";
  13. format.size = 14;
  15. //////////////////////////////////////////////////////Dynamic Text Field
  16. var tf:TextField = new TextField();
  17. tf.embedFonts = true; //<----------------------------embedFonts must be true;
  18. tf.selectable = false;
  19. tf.x = 8;
  20. tf.y = 20;
  21. tf.defaultTextFormat = format; //<-------------------Must be defaultTextFormat;
  22. tf.text = "A new font";
  23. addChild(tf);
  25. //////////////////////////////////////////////////////Text Field already on stage titled testText1
  26. testText1.defaultTextFormat = format;
  27. testText1.text = "A new font";
  28. testText2.embedFonts = true; //<--------------------embedFonts must be true;
  29. testText2.defaultTextFormat = format2; //<-----------Must be defaultTextFormat;
  30. testText2.text = "A new font";
  32. //////////////////////////////////////////////////////If Using AssetManagement Solution
  34. import AssetManager;
  36. public class Main extends MovieClip
  37. {
  38. var FT:Class = AssetManager.FONT_NEOSANS; //<-------Gives access to font in AssetManager class.
  39. public function Main()
  40. {
  41. // Continue with TextFormat and Text Field code above.
  42. }
  43. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.