2009-02-13

The notorious Problem with letterSpacing in Flash

These days I met a problem in Flash with the property letterSpacing of a dynamic text field.

If we are looking at the examples provided by Adobe:

this.createTextField("mytext", this.getNextHighestDepth(), 10, 10, 200, 100);
mytext.multiline = true;
mytext.wordWrap = true;
mytext.border = true;

var format1:TextFormat = new TextFormat();
format1.letterSpacing = -1;

var format2:TextFormat = new TextFormat();
format2.letterSpacing = 10;

mytext.text = "Eat at \nJOE'S.";
mytext.setTextFormat(0, 7, format1);
mytext.setTextFormat(8, 12, format2);

The problem is, this doesn't work for a dynamic text field, only for dynamic generated text field!

There are a lot of solutions on the internet.

Solution 1:

var
styling:TextFormat = new TextFormat();
styling.font = "Blackadder";
styling.color = 0xBA1424;
styling.letterSpacing = 15;

tf.setNewTextFormat(styling);
tf.text="mööööp"


This one doesn't use the method setTextFormat() to set the letterSpacing, instead of that, setNewTextFormat() will be used.

Solution 2:

From the blog "Summit Projects":

function setTextFormatting(){
var fmt:TextFormat = club_name.name.getTextFormat();
club_name.name.setTextFormat(fmt);
club_name.name.setNewTextFormat(fmt);
club_name.name.autoSize = “left”;
}


He said, "It’s silly, but it works.". Unfortunately, it didn't work in my case.

Solution 3:

Bob Walton suggests that in his blog "Flash: yourMom.getTextFormat(); //is the key to letterSpacing"

var fmt:TextFormat = myTextField.getTextFormat();
myTextField.setTextFormat(fmt);
myTextField.setNewTextFormat(fmt);


This also didn't work for me.

I spent hours to figure out how can I fix it. Now here is the solution from mine.

txtCtrl.html = true;
txtCtrl.htmlText = "<font letterspacing='-3'>Now it is the right one!</font>";


So you can see, I got no sucess on setting the format of a dynamic text field. At the end, a font property can achieve that so easily!