Saturday, August 6, 2011

Letter Spacing - TextFormat or css StyleSheet

If you;re looking for example of setting letter spacing - here it is.

The first solution is TextFormat. The property is named same in both options - letterSpacing. Let's see the code:

var txtfmodified:TextField = new TextField();
addChild(txtfmodified);
txtfmodified.y =20;
txtfmodified.text = 'hello world';
var tf:TextFormat = txtfmodified.getTextFormat();
tf.letterSpacing = 3; /* I tried lots of different values here. */
txtfmodified.setTextFormat(tf);

We can also use stylesheet to adjust part of text (in previous solution you can also adjust part of text but selecting chars is more complicated than using styles for classes of html tags). Here is the example:

var txtfmodified2:TextField = new TextField();
addChild(txtfmodified2);
txtfmodified2.y =40;
txtfmodified2.text = 'hello world';
var s:StyleSheet = new StyleSheet();
s.setStyle("p", {letterSpacing:3});
txtfmodified2.styleSheet = s;
txtfmodified2.htmlText = "<p>hello world</p>";

Remember that there are two solutions but you cannot mix them. Either way you'll receive error like this:
Error: Error #2009: This method cannot be used on a text field with a style sheet


And a proof at the end. First line is simple not modified field. Second line is modified by TextFormat properties. Third line is adjusted by stylesheet.




1 comment: