lehermj
lehermj

Reputation: 936

How do I add style to a text field?

When I press a button it prints a random string in the TextField, but I want to add style too. So I used TextFormat but it doesn't seem to be working. I'm not sure what is wrong, it just seems to ignore the text format.

import flash.text.TextField;
import flash.text.TextFormat;
var tf: TextField = new TextField();
var format: TextFormat = new TextFormat();
format.size = 18;
format.align = TextFormatAlign.CENTER;
format.bold = true;
format.color = 0x555555;
tf.setTextFormat(format);

convob.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event: MouseEvent): void {

    starters();


addChild(tf);

}

function starters() {

var random = Math.floor((Math.random() * 5) + 1);

switch (random) {

    case 1:
        tf.text = "eiff";
        addChild(tf);
        break;
    case 2:
        tf.text = "fne";
        addChild(tf);
        break;
    case 3:
        tf.text = "fnfefe";
        addChild(tf);
        break;
    case 4:
        tf.text = "fnefewwe";
        addChild(tf);
        break;
    case 5:
        tf.text = "fneeeffvvv";
        addChild(tf);
    default:
        break;
}

}

Upvotes: 0

Views: 78

Answers (2)

Ribs
Ribs

Reputation: 1505

setTextFormat() will not set text inserted by the user or programatically. To get your text formatting to show up correctly use defaultTextFormat() after text insertion:

function starters() {

    var random = Math.floor((Math.random() * 5) + 1);

    switch (random) {

        case 1:
            tf.text = "eiff";
            addChild(tf);
            break;
        case 2:
            tf.text = "fne";
            addChild(tf);
            break;
        case 3:
            tf.text = "fnfefe";
            addChild(tf);
            break;
        case 4:
            tf.text = "fnefewwe";
            addChild(tf);
            break;
        case 5:
            tf.text = "fneeeffvvv";
            addChild(tf);
        default:
            break;
    }

    tf.defaultTextFormat = format;

}

Upvotes: 0

Zhafur
Zhafur

Reputation: 1624

Try the tf.setTextFormat(format); function with which you can even set only parts of the text field if you wish to.

Upvotes: 1

Related Questions