bukzor
bukzor

Reputation: 38532

AS3: render plain text in a textField

The current behavior of textField is to render all text as if it were HTML. Is there a way to tell the system that this is plain text, even if it looks like html?

In AS2 there was a .html property which I could set to false to get this behavior, bit it seems to have disappeared.

I'm well aware that I can html-escape the input to get the desired behavior, but I'm looking for the cleanest / simplest solution. (Also it seems that I'd have to write my own html-escape function...)

Upvotes: 0

Views: 684

Answers (1)

Alexis King
Alexis King

Reputation: 43902

The AS3 text field has two properties, text and htmlText. The text property can be used to display purely plain text, even if it looks like html. The two properties are automatically linked by Flash, so editing text will update htmlText and vice-versa.

You should be able to use the text property to achieve your goal. Unfortunately, if you are using a style sheet with your text field, both properties will be treated as HTML. In this case, you have two choices. You can perform the styling without a style sheet, using simple TextFormat objects, which may be simple if your stylesheet is not too complex. If you really need to use a stylesheet, you can take advantage of AS3's link between the two text properties to escape your text automatically.

For example, if you wanted to update a text field called tf which uses a style sheet, but you wanted plain text, you could do it like this:

var temp:TextField = new TextField();
temp.text = plainText;
tf.text = temp.htmlText;

The temporary text field will automatically generate escaped HTML, which you can then use in your actual text field.

Upvotes: 4

Related Questions