On Not | Mo Chit

April 01, 2004

Flash: TextFormat to HTML Have you ever wanted to see the HTML representation of a TextFormat object without having to go and create a textfield, set the format, and then get the htmlText?

Well the code below creates a single hidden textfield and then calling fmtToHtml takes care of all the details:
//create a default textformat
var $DefaultFmt = new TextFormat("Times New Roman",12,0x000000,false,

//create the hidden textfield

//initialize it
var $ConvTxt = _root.$ConvTxt;
$ConvTxt.visible = false;
$ConvTxt.html = true;

//takes an input string and format and converts it to html
_global.fmtToHtml = function(txt:String,fmt:TextFormat):String {
  //asign text
  $ConvTxt.text = txt;
  //reset all formating
  //apply new format
  return $ConvTxt.htmlText;
You'll notice that before applying the TextFormat, it's first reset with a default format. This is necessary because when apply a new TextFormat, it is merged with the existing format. For example if the currently applied format had the bold property set to true, and you applied a new format without explicitly setting bold to false, the resulting format would still be bold.
Posted by Wes at 04:51 PM

Creative Commons License
This site is licensed under a
Creative Commons License