8.10. Displaying HTML-Formatted Text

Problem

You want to display HTML content in a text field.

Solution

Set the text field’s html property to true, and set the htmlText property to the value of the HTML content to display.

Discussion

Text fields can interpret and display basic HTML tags, if properly configured. Using HTML in a text field is a convenient way to add links and simple formatting, such as font color and bold text.

Text fields display plain text by default. To enable HTML formatting, set the field’s html property to true:

myTextField.html = true;

Once the html property is set to true, the value of the object’s htmlText property will be interpreted as HTML:

myTextField.htmlText = "<u>this will display as underlined text</u>";

Set the html property to true before setting the htmlText property; otherwise, the value of htmlText will not be interpreted as HTML.

When the html property is false (which is the default), the htmlText property value is rendered as regular text. But when html is set to true, the htmlText property is rendered as HTML complete with <p> and <font> markup tags:

myTextField.html = false;
myTextField.htmlText = "test";
trace(myTextField.htmlText);
/* Output window displays:
test
*/
myTextField.html = true;
myTextField.htmlText = "test";
trace(myTextField.htmlText);
/* Output window displays:
<P ALIGN="LEFT"><FONT FACE="Times New Roman" 
    SIZE="12" COLOR="#000000">test</FONT></P>
*/

No matter what, the text property of a text field is rendered as plain text. This means ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.