146 Chapter 8: Working with Text
You could then populate a text field associated with that style sheet with the following XML-
formatted text:
<sectionHeading>This is a section</sectionHeading>
<mainBody>This is some main body text,
with one <emphasized>emphatic</emphasized> word.
</mainBody>
An example of using styles with XML
In this section, you’ll create the same FLA file that you created earlier (see “An example of using
styles with HTML” on page 144) but with XML-formatted text. In this example, you’ll create the
style sheet using ActionScript, rather than importing styles from a CSS file.
To format XML with a style sheet:
1 In Flash, create a FLA file.
2 Using the Text tool, create a text field approximately 400 pixels wide and 300 pixels high.
3 Open the Property inspector (Window > Properties) and select the text field.
4 In the Property inspector, select Dynamic Text from the Text Type menu, select Multiline from
the Line Type menu, select the Render Text as HTML option, and type
news_txt in the
Instance Name text box.
5 On Layer 1 in the Timeline (Window > Timeline), select the first frame.
6 To create the style sheet object, open the Actions panel (Window > Development Panels >
Actions) and add the following code to the Actions panel:
var xml_styles = new TextField.StyleSheet();
xml_styles.setStyle("mainBody", {
color:'#000000',
fontFamily:'Arial,Helvetica,sans-serif',
fontSize:'12',
display:'block'
});
xml_styles.setStyle("title", {
color:'#000000',
fontFamily:'Arial,Helvetica,sans-serif',
fontSize:'18',
display:'block',
fontWeight:'bold'
});
xml_styles.setStyle("byline", {
color:'#666666',
fontWeight:'bold',
fontStyle:'italic',
display:'inline'
});
xml_styles.setStyle("a:link", {
color:'#FF0000'
});
xml_styles.setStyle("a:hover", {
textDecoration:'underline'
});
This code creates a new style sheet object named xml_styles that defines styles by using the
setStyle() method. The styles exactly match those you created in an external CSS file earlier
in this chapter.