AS3 TextField and StyleSheet Example (created in ActionScript)


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



Copy this code and paste it in your HTML
  1. import flash.text.StyleSheet;
  2. import flash.text.TextField;
  3. import flash.text.TextFieldAutoSize;
  4.  
  5. var myHTMLText:String = "<span class='defaultStyle'><h1><b>HTML</b> Text <i>(sample <u>header</u>)</i></h1>Here is some <em>sample</em> <strong>html text</strong> "+"filling a text box <a href='http://www.adrianparr.com'>this link to adrianparr.com</a> and example headers"+"<br><br><br><h1>Header h1</h1><h2>Header h2</h2><br><br><br>Hello world<br><br><br><redText>This text <i>will be red</i></redText><br><br><h1>Boo</h1></span>";
  6.  
  7. var defaultStyleObj:Object = new Object();
  8. defaultStyleObj.fontFamily = "Verdana";
  9.  
  10. var myStyleSheet:StyleSheet = new StyleSheet();
  11. myStyleSheet.setStyle("body", {fontSize:'15',color:'#000066'});
  12. myStyleSheet.setStyle("h1", {fontSize:'32',color:'#000000'});
  13. myStyleSheet.setStyle("h2", {fontSize:'19',color:'#000000'});
  14. myStyleSheet.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
  15. myStyleSheet.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
  16. myStyleSheet.setStyle("b", {fontWeight:'bold'});
  17. myStyleSheet.setStyle("em", {fontWeight:'bold'});
  18. myStyleSheet.setStyle(".defaultStyle", defaultStyleObj);
  19. myStyleSheet.setStyle("redText", {color:'#FF0000'});
  20.  
  21. var myTextField:TextField = new TextField();
  22. myTextField.width = 500;
  23. myTextField.multiline = true;
  24. myTextField.styleSheet = myStyleSheet;
  25. myTextField.htmlText = myHTMLText;
  26. myTextField.autoSize = TextFieldAutoSize.LEFT;
  27. myTextField.wordWrap = true;
  28. myTextField.border = true;
  29. addChild(myTextField);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.