Flash: TextField.htmlText and Automatic Line Breaks

Problem :: When you deal with TextField.htmlText don’t append any html strings directly to htmlText property of TextField. Because the TextField appends “<br>” tag automatically before it appends any html string to htmlText property, it will set your string automatically in next line instead of the current line. This mistake is done to save memory as we don’t have to create a temporary string.

Example :

textField.htmlText = “”;                 //clear any previous texts

for(var i:uint = 0; i < 3; i++)
{
var str:String = “”;
str += “Hello World”;
str += “<br>”;                    //As you’re thinking of breaking line from here
textField.htmlText += str;
}

//output would be something like following ::
Hello World
//This line break because of <br> tag
Hello World         //This is an automatic line break
//This line break because of <br> tag
Hello World         //This is an automatic line break
//output finished

Possible Solution :: Create an empty string and append all the html strings to this string. When the final string is ready, assign this string to htmlText property of TextField. This will not create any unnecessary line breaks inside TextField.

Example :

textField.htmlText = “”;                 //clear any previous texts
var htmlStr:String = “”;                  //Temporary empty string

for(var i:uint = 0; i < 3; i++)
{
var str:String = “”;
str += “Hello World”;
str += “<br>”;                    //As you’re thinking of breaking line from here
htmlStr += str;
}

textField.htmlText = htmlStr;

//output would be something like following ::
Hello World
Hello World
Hello World
//output finished

Hope this would prevent someone from mistakes which I did :(

5 Responses to this post.

  1. Posted by Gohan on January 7, 2009 at 2:03 pm

    Great,
    helped me big time,
    thanks man

    Reply

  2. Posted by Jacob on February 5, 2009 at 3:24 pm

    Thanks for clearing that up, I was a bit confused..
    Although when dealing flash, confusion is part of the ordinary.

    Reply

  3. this works fine!!

    _root.txtResumen.multiline = true;
    _root.txtResumen.wordWrap = true;
    _root.txtResumen.html = true;

    // this is the secret!!
    _root.txtResumen.condenseWhite = true

    Reply

  4. Posted by Lauren on July 6, 2009 at 11:07 pm

    condenseWhite!! Yes!!! Thankyou :)

    Reply

  5. Yeah!

    TextField.htmlText and extra line Breaks :

    myTextField.condenseWhite = true;

    Worked like a charm !

    Reply

Respond to this post