Style htmlText with CSS in your Actionscript | Flash/CSS 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

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://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

This entry was posted in tutorial and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

21 Comments

  1. Posted June 6, 2008 at 12:58 pm | Permalink

    Excellent improvement.

  2. Posted September 6, 2008 at 4:50 pm | Permalink

    Thank you, very straightforward example!

  3. Hz
    Posted October 20, 2008 at 2:43 pm | Permalink

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

  4. Posted October 20, 2008 at 9:54 pm | Permalink

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

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

    Thanks for pointing that out Hz

  5. mata
    Posted October 27, 2008 at 4:39 pm | Permalink

    cool one thanks

  6. Posted January 14, 2009 at 9:34 am | Permalink

    Great tutorial, was a great help,

    Many thanks,

    Saf

  7. David
    Posted May 15, 2009 at 1:20 pm | Permalink

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

    • Posted May 17, 2009 at 10:05 am | Permalink

      @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

  8. luigi
    Posted July 8, 2009 at 6:24 am | Permalink

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

  9. circlecube
    Posted July 8, 2009 at 3:32 pm | Permalink

    @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!

  10. karstor
    Posted December 9, 2009 at 12:50 pm | Permalink

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

  11. Gav
    Posted January 8, 2010 at 9:27 pm | Permalink

    You didn’t define data type for myHtml.

    • Posted January 8, 2010 at 11:01 pm | Permalink

      @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?

      • Gav
        Posted January 10, 2010 at 1:04 pm | Permalink

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

      • Gav
        Posted January 10, 2010 at 1:07 pm | Permalink

        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!

  12. DIOGO
    Posted February 10, 2011 at 12:55 pm | Permalink

    muito bom mais uma pergunta

    como seria para retornar de uma xml com campo assim

    Apollo teste
    campdafjsdfjasçdfasd
    fasdkfasldkjfasd

    Portuguese to English translation:
    very good one more question

    would like to return from a well field with xml

    Apollo Test
    campdafjsdfjasçdfasd

  13. Posted May 1, 2011 at 4:00 pm | Permalink

    Nice and simple tutorial. Did you writte one for actionscript 3 ?

  14. Posted June 20, 2011 at 7:54 pm | Permalink

    The hover functionality happens regardless of whether there is a display object in front of the text or not (the TextEvent.LINK doesn’t fire when you click the text at least so that good). I found this out when using the “hoverable” text beneath a modal window. Anyone know a trick to keep this from happening? I don’t want to have to change my styles on the fly depending on if there is a DisplayObject in front of my text or not.

  15. Posted June 27, 2011 at 6:08 am | Permalink

    Hi Really… Thanks Helped Me lot

  16. Thomas
    Posted June 28, 2011 at 9:36 pm | Permalink

    after close each tag,it is going next line

    could you Please tell me the solution?

    Thanks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

    Developing for old browsers is (almost) a thing of the past – (37signals)

    Basecamp announces that it’s new version will not support old browsers! They will of course continue to support them in their “classic” version. But this is good news and as more implement this type of policy the internet will be happy. It used to be one of the biggest pains of web development. Juggling different [...]

    Screen shot 2012-02-10 at 12.59.33 PM

    Vendor Prefixes – about to go south

    What are “standards” coming to? Who’s guilty? Apple and Chrome: They’re supporting vendor prefixed properties like they’re a standard part of development. Firefox, Opera and Internet Explorer: They should have been on the ball more. Need to push their evangelism further. Teach developers that it’s not exclusively -webkit to style elements. All the browsers: experimental [...]

    bio

    An Event Apart Notes: Ethan Marcotte, Responsive Web Design

    Ethan Marcotte has become the father of Responsive Web Design and spent this whole day focused on principles, techniques, gotchas, examples, … all about building and how to build responsive sites. With a sprinkle of mobile first. For Ethan, it all started with this article: http://www.alistapart.com/articles/dao/ Think of architecture, the whole design phase is established [...]

    Webcomm_Montreal

    An Event Apart Notes: Jared Spool, The Curious Properties of Intuitive Web Pages

    Senseless waste of asterisks… Avis used an asterisk to denote optional fields. This means that there is a lot of baggage that comes with an asterixk. Somewhere this symbol got meaning, it’s not in the bible! We can control when something goes from unintuitive to intuitive. A design is intuitive (although technically and grammatically speaking [...]

    untitled-158-2

    An Event Apart Notes: Marco Arment, Bridging the App Gap

    The iPhone changed our industry in 2007: first mobile to have a desktop class web browser and it made people start using their mobile phones as computers! All apps other than apple provided ones were web browser apps. Most of the first apps were branded web browsers. No real difference between using mobile site or [...]

    lukew_space

    An Event Apart Notes: Luke Wroblewski, Mobile to the Future

    MASS MEDIA (in waves) Print, Recordings, Cinema, Radio, Television, Internet, Mobile Mobile is the new Mass Media It certainly is mass and massive. More mobile devices (by far) every day than babies born on the planet. Mobile devices are eating into our personal computing shares. New waves of mobile media eat all previous media waves. [...]

    IMG_4500

    An Event Apart Notes: Josh Clark, Buttons are a Hack

    Mobile and touch should be revolutionizing design and user experience. We don’t want to touch a tiny link or button. Back button is just stupid. Fitz’s Law, make things fat proximal targets for easy touching. People are lazy, let’s as designers LET people be lazy! Maybe even use the whole screen as your control. Eliminate [...]

    nicole_pink_bkg_2012

    An Event Apart Notes: Nicole Sullivan, Our Best Practices are Killing Us

    Grep to for analyze css. CSS duplication is a web-wide problem. Started helping facebook optimize thier site and they had 1.9MB of css loading. The same color showing up hundreds of times. Many many color statements and declarations. !important declarations get dangerous. Sites found to have over 500 !important declarations! float is a serious problem [...]

    ericmeyer

    An Event Apart Notes: Eric Meyer, The Future of CSS

    Cormorant trained to fish for the fishermen. The fishermen tie bands around the birds necks that are. Fishermen fish at night, using lamps they attract bugs, which attract fish, and the cormorant get to fish but cant swallow them and the fishermen take the fish. Browser vendors are promising us new CSS tools, but don’t [...]

    sammyj

    An Event Apart Notes: Ethan Marcotte, Rolling Up Our Responsive Sleeves

    Henry Adams (Descendant of 2 presidents: great-grandson to John Adams and grandson to John Quincy Adams). He lived between the civil war and world war 1. He witnessed the industrial revolution. Chaos was the law of nature, Order was the dream of man Samuel Johnson – funniest man in the 17th Century… Responsive Design: 1. [...]

  • Recent Comments

    me-gamer

    me-gamer

    this is really nice. this made many of my task simple.
    Lori Newman

    Lori Newman

    Just wanted to thank you for your presentation. It was extremely informative and just what I...
    Karl

    Karl

    I have been using for some time this nice Banner, from developer FX. They have a really nice Live...
    Karl

    Karl

    Thank you for this wonderful link… recommend it! Fast, simple, easy… :-)
    Gabriel

    Gabriel

    Hi Valerie, I don’t know if you are still following this post, but I tried seeing if it is...
    avinash

    avinash

    Hi Evan, I am using the same code and trying it on chrome/firefox it is not working on neither...