Question

Is it possible to apply StyleSheet formatting to a Flash TextField that has only a plain text set either via text or htmlText properties? In the documentation to the TextField#styleSheet property, this example is given:

 public class TextStylesheetExample extends Sprite {
    var myLabel:TextField = new TextField();
    var labelText:String = "Hello world.";
    var newStyle:StyleSheet = new StyleSheet();

    public function TextStylesheetExample()
    {
        var styleObj:Object = new Object();
        styleObj.fontWeight = "bold";
        styleObj.color = "#660066";
        newStyle.setStyle(".defStyle", styleObj);

        myLabel.styleSheet=newStyle;
        myLabel.htmlText=labelText;
        addChild(myLabel);
    }
}

When I run this code in Flash Player 12 / AIR 4, I see only unformatted text. I see some results if the code is changed to something like

var labelText:String = "<p>Hello world.</p>";
...
newStyle.setStyle("p", styleObj);

I.e., when selectors come to play, it starts working. However, in my usecase, I have just plain texts without any formatting tags and am wondering whether it's possible to style something like text="Hello World" with a StyleSheet object. Documentation suggests that it should be but I can't get it working.

Thanks.

Was it helpful?

Solution

In a TextField, non-HTML text is actually placed within a set of paragraph tags. As an experiment:

var tf:TextField = new TextField();
tf.text = "hello world";
trace(tf.htmlText);

The trace output will be:

<P ALIGN="LEFT"><FONT FACE="Times New Roman" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">hello world</FONT></P>

Because there is a nested FONT element that "hardcodes" the styling information, the answer to the original question is no, plain text TextFields cannot have their font properties styled using a StyleSheet. Use defaultTextFormat or setTextFormat() instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top