Using HTML-formatted text 147
7 To create the XML text to assign to the text field, add the following code to the Actions panel:
var storyText = "<title>Flash Player now supports CSS</
title><mainBody><byline>San Francisco, CA</byline>--Macromedia Inc.
announced today a new version of Flash Player that supports Cascading
Style Sheets (CSS) text styles. For more information, visit the <a
href=\"http://www.macromedia.com\">Macromedia Flash website</a></
mainBody>";
8 Last, add the following code to apply the style sheet object to the text field’s styleSheet
property and assign the XML text to the text field.
news_txt.styleSheet = xml_styles;
news_txt.text = storyText;
9 Save the file as news_xml.fla.
10 Run the movie (Control > Test Movie) to see the styles automatically applied to the text in
the field.
Using HTML-formatted text
Flash Player supports a subset of standard HTML tags such as <p> and <li> that you can use to
style text in any dynamic or input text field. Text fields in Flash Player 7 and later also support the
<img> tag, which lets you embed JPEG files, SWF files, and movie clips in a text field. Flash
Player automatically wraps text around images embedded in text fields in much the same way a
web browser wraps text around embedded images in an HTML page. For more information, see
“Embedding images, SWF files, and movie clips in text fields” on page 152.
Flash Player also supports the
<textformat> tag, which lets you apply paragraph formatting
styles of the TextFormat class to HTML-enabled text fields. For more information, see “Using the
TextFormat class” on page 137.
Overview of using HTML-formatted text
To use HTML in a text field, you must enable the text field’s HTML formatting either by
selecting the Render Text as HTML option in the Property inspector, or by setting the text field’s
html property to true. To insert HTML into a text field, use the TextField.htmlText property.
For example, the following code enables HTML formatting for a text field named
headline_txt,
and then assigns some HTML to the text field.
headline_txt.html = true;
headline_txt.htmlText = "<font face='Times New Roman' size='24'>This is how
you assign HTML text to a text field.</font>";
Attributes of HTML tags must be enclosed in double or single quotation marks. Attribute values
without quotation marks may produce unexpected results, such as improper rendering of text.
For example, the following HTML snippet will not be rendered properly by Flash Player because
the value assigned to the
align attribute (left) is not enclosed in quotation marks:
textField.htmlText = "<p align=left>This is left-aligned text</p>";
If you enclose attribute values in double quotation marks, you must “escape” the quotation marks
(
\"). For example, either of the following are acceptable:
textField.htmlText = "<p align='left'>This uses single quotes</p>";
textField.htmlText = "<p align=\"left\">This uses escaped double quotes</p>";
It’s not necessary to escape double quotation marks if you’re loading text from an external file; it’s
only necessary if you’re assigning a string of text in ActionScript.