Dynamic CSS text in Flash

28

Posted by VitaminB | Posted in Flash, Source Downloads | Posted on 23-06-2008

Tags: , , ,

In this tutorial I will show you how to load dynamic text into your flash document and then format it with CSS. This is really useful for a news section or text that regularly needs updating.

Overview

We will create a dynamic text field in Flash. Then load a normal text document formated with HTML tags. Finally we will apply the CSS to the dynamic text field.

The text document

1. Create a new text document with notepad or any text editor of your choice.
2. Paste the following text as is in the new document.

3. As you can see we start the document with myVariable= . This will be used by flash to determine that the following text will be loaded into our dynamic text field. The rest of the document has got standard HTML tags embedded.

4. Save the text document as myText.txt

Flash

1. Create a new flash document with a dynamic text field. Give the text field an instance name of myText_txt and select the Render text as HTML tab. Remember to allow enough space for the text so draw out a big enough text area for as much text as you want to load.

2. Create a new layer and name it Actions.

3. On the first frame of the Actions layer place the following code.
var myStyle = new TextField.StyleSheet();
myStyle.load("myStyle.css");
myStyle.onLoad = function() {
myText_txt.styleSheet = myStyle;
};

As you can see we first load the stylesheet before we load the dynamic text. If you do it the other way around not all styles gets loaded correctly.

4. Next we create a function to load the dynamic text into flash. Add the following code beneath the previous block.
function textLoader() {
myData = new LoadVars();
myData.onLoad = function() {
myText_txt.html = true;
myText_txt.htmlText = this.myVariable;
};
myData.load("myText.txt");
}

5. Now we can load the text document we created earlier on by calling the textLoader() function. Add the following code.

textLoader();

The CSS file

I will not go into depth about creating the CSS file as this tutorial is not aimed at that. There are loads of tutorials about CSS with a simple search in google.

1. You can paste the following CSS code into a normal text document as is.

p{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color:#999999;
}

.header{
font-size: 24px;
color:#FF9933;
}

.footer{
font-family:Geneva, Arial, Helvetica, sans-serif;
text-align:right;
}

a{
color:#FF0000;
text-decoration:underline;
font-weight:bold;
font-size: 10px;
}

2. Save the file as myStyle.css in the same folder as the myText.txt and the csstext.swf file.

That should do it. Back in flash, Compile the document and your text should load dynamically and be formatted accordingly.
You can view an example here

Source available on request.

Comments (28)

Hi, This works a charm, can I assign this to more than one text field? Thank you I never thought that you can use CSS with flash

Excellent !!! This brings a whole lot of new ideas I’m going to try… Great post !!

Cool Thank you !!!

Cheers Mate !!! This will help a lot…

WOOOOAAAA!!! nice..

Cool, this will definately come in handy…. Cheers mate

Very useful, thanks

Very nice and easy to follow, cheers

Wicked !!!

Excellent thank you, works perfectly for me.

Can you show me how to apply css to xml using multiple text fields?

Hi Brian,

You can not really apply CSS to XML but the same method would be used to apply the CSS to the text field that the XML is loaded into. Just add the 2nd textfield that you want formatted to the AS. myText2_txt…

What version of actionscript is this written in? I am new to the Flash world

Hi Sarah, this is written in ActionScript 2.0. Are you getting it to work? You can download the source here :
DOWNLOAD

anybody here know of a good site to find more info on free flash website tutorial? I\’ve got this site bookmarked and im gonna keep checking it out, but i still would like to find a site that covers free flash website tutorial a little more thoroughly..thanks

Very explicit tutorial. I came across several similar tutorials on the web that would not work. This one is clear and works fine. Thank you.

hey,
this tutorial was fantastic its working like a charm but just have a couple of questions.

I would like to use symbols for example, the currency sign (euro, €, €) and the amp symbol (&, &) is it possible by any chance?

gleich musik und filme downloaden…

Kennt jmd. ein website wo ich die Fernsehserie wie “Stargate” downloaden kann?…

hi,

so it works fine..but for some reason, when i load the csstext.swf from an xml file, the content shows up as undefined. if i open up the csstext.swf directly into the window it works like a charm…

any guesses?

regards

go

i tested ur code as it is in the tutorial. but it didn’t work for me.
Can i have the source please

Hi you can download it from http://www.brianwiltshire.net/new – Select Flashfiles and then desktop.

I have a question about spacing… using your tutorial I have created the desired result but I find spacing between paragraphs to be very large. I have tried using “line-height: 1px;” In the styles but it seems to not work. Any advise?

@JP – You can specify it on the text field within flash. That should do it.

Had the same problem, got it working, Thank you so much for the share, Also your other files. Keep up the good work !!

I have a problem with letter-spacing, this don´t work. Can you help me?
tanks :)

What do you need help with? send me your source and I will see what I can do.

thank you so much for this l’simple’ code, but yet awesome effect!!!

Grrrrrrrrrreat !

Write a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes