AS3 Loading and Using an External CSS File


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



Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import flash.net.URLLoader;
  5. import flash.net.URLRequest;
  6. import flash.text.StyleSheet;
  7. import flash.text.TextField;
  8. import flash.text.TextFieldAutoSize;
  9.  
  10. public class CSSFormattingExample extends Sprite {
  11. var loader:URLLoader;
  12. var field:TextField;
  13. var exampleText:String = "<h1>This is a headline</h1>" +
  14. "<p>This is a line of text. <span class='bluetext'>" +
  15. "This line of text is colored blue.</span></p>";
  16.  
  17. public function CSSFormattingExample():void {
  18. field = new TextField();
  19. field.width=300;
  20. field.autoSize=TextFieldAutoSize.LEFT;
  21. field.wordWrap=true;
  22. addChild(field);
  23.  
  24. var req:URLRequest=new URLRequest("example.css");
  25.  
  26. loader = new URLLoader();
  27. loader.addEventListener(Event.COMPLETE, onCSSFileLoaded);
  28. loader.load(req);
  29. }
  30.  
  31. public function onCSSFileLoaded(event:Event):void {
  32. var sheet:StyleSheet = new StyleSheet();
  33. sheet.parseCSS(loader.data);
  34. field.styleSheet=sheet;
  35. field.htmlText=exampleText;
  36. }
  37. }
  38. }
  39.  
  40.  
  41. // THE 'example.css' FILE SHOULD LOOK LIKE THIS ...
  42. //p {
  43. // font-family: Times New Roman, Times, _serif;
  44. // font-size: 14;
  45. //}
  46. //
  47. //h1 {
  48. // font-family: Arial, Helvetica, _sans;
  49. // font-size: 20;
  50. // font-weight: bold;
  51. //}
  52. //
  53. //.bluetext {
  54. // color: #0000CC;
  55. //}

URL: http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000232.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.