TextField.htmlText Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
TextField.htmlText Property Flash 6

HTML source code to render in the text field read/write
theField.htmlText

Description

Like the text property, htmlText is used to specify text for on-screen display but with the added feature that if the html property is true, HTML tags in htmlText are interpreted as formatting instructions. To display formatted HTML in a text field, first set html to true, and then assign HTML source code to htmlText:

this.createTextField("theField_txt", 1, 0, 0, 200, 20); theField_txt.html = true; theField_txt.htmlText = '<B>This will display bold text.</B>';

The set of supported tags is limited, compared to a web browser, but it still provides good basic text control. See Appendix E for details.

Assigning a value to htmlText overwrites the value of text, and vice versa. In most cases, if you are using HTML text, set the html property to true and access the field's text using the htmlText property. If you are using non-HTML text, set the html property to false and access the field's text using the text property.

When html is false, HTML tags in text assigned to htmlText show up as regular text, and htmlText behaves in the same way as the text property. HTML tags assigned directly to text are never interpreted as HTML; they are always displayed verbatim.

However, when html is true and HTML tags are assigned to htmlText, the value of text will be that of htmlText, but with all HTML tags stripped out. For example, here we create an HTML-enabled text field and assign some HTML to its htmlText property for display:

this.createTextField("theField_txt", 1, 0, 0, 200, 20); theField_txt.html = true; theField_txt.htmlText = '<P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12"'                         + 'COLOR="#000000">This field contains <B>HTML!</B>'                         + '</FONT></P>';

After the assignment, htmlText has the value:

<P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000"> This field contains <B>HTML!</B></FONT></P>

But text has the value:

This field contains HTML!

Conversely, as long as html is true, Flash automatically adds HTML markup to htmlText if the HTML source assigned to htmlText does not fully describe the text field's formatting. For example, here we set htmlText without using any <P> or <FONT> tags:

theField_txt.htmlText = "This field contains <B>HTML!</B>";

When we examine the value of theField_txt.htmlText, we find that <P> and <FONT> tags have been added:

trace(theField_txt.htmlText); // Displays: <P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000"> This field contains <B>HTML!</B></FONT></P>

Meanwhile, text has the value:

This field contains HTML!

When theField.html is true and theField is formatted with setTextFormat( ), htmlText represents the formatting with HTML tags. For example:

this.createTextField("theField_txt", 1, 0, 0, 200, 30); theField_txt.html = true; theField_txt.text = "Stretch often while you work."; // Create a TextFormat object that specifies bold text. boldFormat = new TextFormat(); boldFormat.bold = true; boldFormat.size = 20; // Apply the format to the word "often", characters 8 to 12 (inclusive). theField_txt.setTextFormat(8, 13, boldFormat); // Check the value of htmlText. Note the <B> tags around the word "often". trace(theField_txt.htmlText); // Displays: <P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000"> Stretch <FONT SIZE="20"><B>often</B></FONT> while you work.</FONT></P>

Note, however, that the new text format for theField (as set by setNewTextFormat( )) is not used by htmlText, as demonstrated in the following code:

this.createTextField("theField_txt", 1, 0, 0, 250, 100); theField_txt.html = true; // Set the new text format to be bold, 20 pt. tformat = theField_txt.getNewTextFormat(); tformat.size = 20; tformat.bold = true; theField_txt.setNewTextFormat(tformat); // This text, assigned to htmlText, does not take the formatting theField_txt.htmlText = '<P>This text is size 12, not size 20.</P>'; // This text, assigned to text, will be size 20, bold theField_txt.setNewTextFormat(tformat); theField_txt.text = 'This text is size 20, bold.';

Table 18-23 shows the effect of setting the htmlText property under different scenarios.

Table 18-23. Display when setting the htmlText property

html

Assigned to htmlText

htmlText contents

text contents

On-screen display

false

<I>Italicized text</I>

<I>Italicized text</I>

<I>Italicized text</I>

<I>Italicized text</I>

false

Plain text

Plain text

Plain text

Plain text

true

<I>Italicized text</I>

<P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000"><I>Italicized text</I></FONT></P>

Italicized text

Italicized text

true

Plain text

<P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000">Plain text</FONT></P>

Plain text

Plain text

Take heed that successive assignments to htmlText and text overwrite each other. Regardless of the value of theField.html, assigning a new value to text overwrites the value of htmlText, and vice versa. Successive concatenations (not reassignments), in contrast, do not overwrite each other:

theField_txt.htmlText = "<B>hello</B>"; theField_txt.text += " world"; // theField_txt.htmlText is now: // <P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" // COLOR="#000000">hello world</FONT></P>

But concatenating text to htmlText can cause problems; when we assigned a value to text, Flash removed our <B> tag assigned to htmlText because assigning a new value to text removes all custom formatting in the field. Further, each concatenation or assignment to htmlText causes the insertion of a new paragraph tag (<P>). Hence, mixing text and htmlText assignments generally is not recommended. Each assignment made to htmlText should comprise an entire paragraph; otherwise, any subsequent concatenation will force an undesired line break.

To include HTML source code in an HTML-enabled text field, convert the characters <, >, &, ", and ' to their equivalent HTML entity values: &lt;, &gt;, &amp;, &quot;, and &apos;, as discussed at: http://moock.org/asdg/technotes/sourceInHtmlField.

To display HTML tags in a non-HTML text field, simply set html to false and use the text property, as follows:

this.createTextField("theField_txt", 1, 0, 0, 200, 20); theField_txt.html = false; theField_txt.text = 'This field contains <B>HTML!</B>';

If the embedFonts and html properties are both true, every font face and variation used in htmlText must have a corresponding font exported with the movie (as described under embedFonts). If we use Arial bold, Arial italic, and Arial bold italic in htmlText, then we must embed all three font variations. For example, suppose we create a text field called output_txt. In the Properties inspector for our output_txt text field we select Arial, set to Italic. In the Character Options dialog box, we embed All Characters of the Arial italic font. Then we set output_txt to display HTML. Finally, we assign the following value to our text field:

output_txt.htmlText = '<P><I>My</I>, what <B>lovely</B>'                     + '<FONT SIZE="24">eyes</FONT> you have!</P>';

When the movie plays, the following text appears in the text field:

My

Everything else we assigned to output is missing! Only the italic text is rendered. The rest of the text requires other variations of the Arial font that we didn't embed "what" and "eyes you have" are not italicized, and "lovely" is bolded.

Underlining is not considered a font variation, nor is font size or color. If, however, embedFonts is false, Flash relies entirely on the user's system for fonts, in which case normal, bold, and italic text will be rendered only if users have the appropriate font variant installed on their systems.

For further discussion of HTML usage in text fields, see Appendix E.

See Also

getURL( ), TextField.embedFonts, TextField.html, TextField.text, the TextFormat class; Appendix E



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net