circlecube

RSS comments
LinkedIn Twitter delicious fb last.fm
5 Jun 2008

Style htmlText with CSS in your Actionscript | Flash/CSS Tutorial

Author: Evan Mullins | Filed under: tutorial

Overview

In flash you can have text areas that are rendered as html. You can also apply formatting styles to this html. This will show a simple example on how to apply css to html text in flash. I’ll do a simple anchor tag style to show you the ropes. We’ll style a link to be underlined and then when you hover or mouse over it, we’ll change the color. It’s a design style that is widely used online in html, but flash doesn’t natively do it. As a matter of fact, flash doesn’t even natively underline links.

Steps

  1. Import TextField.StyleSheet
  2. create a style sheet object: var myCSS:StyleSheet = new StyleSheet();
  3. Specify your styles: myCSS.setStyle(“a:link”, {color:’#0000CC’,textDecoration:’underline’});
  4. Ensure that the text box is html enabled: myHTML.htmlText = myHTMLText;
  5. Apply the style sheet object to your html text box: myHTML.styleSheet = myCSS;

Example

Get Adobe Flash player

Actionscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import TextField.StyleSheet;

myHTMLText = "
<h1>HTML Text (sample header)</h1>
Here is some <em>sample</em> <strong>html text</strong> "
+
"filling a text box <a href="http://blog.circlecube.com">this link to circlecube</a> and example headers"+
"<h1>Header h1</h1><h2>Header h2</h2>";

//create and initialize css
var myCSS:StyleSheet = new StyleSheet();
myCSS.setStyle("body", {fontSize:'15',color:'#000066'});
myCSS.setStyle("h1", {fontSize:'25',color:'#000000'});
myCSS.setStyle("h2", {fontSize:'19',color:'#000000'});
myCSS.setStyle("a:link", {color:'#0000CC',textDecoration:'none'});
myCSS.setStyle("a:hover", {color:'#0000FF',textDecoration:'underline'});
myCSS.setStyle("b", {fontWeight:'bold'});
myCSS.setStyle("em", {fontWeight:'bold'});

//ensure html support and apply css to it
myHTML.html = true;
myHTML.styleSheet = myCSS;
myHTML.htmlText = myHTMLText;
//resize the textbox to exact fit the text in it
//myHTML.autoSize = "left";

Download

open source flashhtmlcss.zip

  • del.icio.us
  • Digg
  • email
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • Print
  • PDF
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS

Tags: , , , , , ,

15 Responses to “Style htmlText with CSS in your Actionscript | Flash/CSS Tutorial”

  1. Excellent improvement.

  2. Thank you, very straightforward example!

  3. I don’t know but i don’t see your tag working.. looks bold to me and not italic.

  4. My bad…
    line 17 in the source code should read:

    1
    myCSS.setStyle("em", {fontStyle:'italic'});

    Thanks for pointing that out Hz

  5. cool one thanks

  6. Great tutorial, was a great help,

    Many thanks,

    Saf

  7. Thanks for the tutorial. I think you should mention that this tutorial is for AS2 somewhere though.

  8. @David – Correct, sorry I didn’t mention it. This is AS2 version, AS3 isn’t too different, I’ll have to put together an article for that. Thanks

  9. hi and thanks
    is possible pick the text from a .txt file?

  10. circlecube says:
  11. @Luigi – Yes, this is possible, I don't remeber the syntax off the top of my head, but a well thought google search should do the trick, good luck!

  12. dude, this is the best, simplest and most straight-forward tutorial I have come accross on the Web. Very well done and many thanks!

  13. You didn’t define data type for myHtml.

  14. @Gav – no requirement to define the datatype in as2. But in as3 that would be needed, should I write a new tut on how to do this in as3?

  15. Thanks Even for your prompt reply, it will be highly appreciated if you could write a new one.

  16. And I also wish to store the html text in XML file, in that case I can maintain html text dynamically. Can I apply CSS to the html text which I get from XML file? Thanks a lot, Evan you are awesome guy and very helpful!

Leave a Reply