here are 2 code blocks.
var myTextField:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat();
addChild(myTextField);
myTextFormat.font = "Arial";
myTextFormat.size = 15;
myTextFormat.color = 0x0000FF;
myTextField.text = "some text";
**myTextField.setTextFormat(myTextFormat);**
VS
var myTextField:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat();
addChild(myTextField);
myTextFormat.font = "Arial";
myTextFormat.size = 15;
myTextFormat.color = 0x0000FF;
**myTextField.defaultTextFormat = myTextFormat;**
So , What’s the difference between
setTextFormat() and defaultTextFormat ? Why two different ways ( one by property another by method) to do things.
Tested with some more code :
var my_txt:TextField =new TextField();
my_txt.type = TextFieldType.INPUT
var my_fmt:TextFormat = new TextFormat();
my_fmt.color = 0xFF0000;
my_txt.text = "this is for setTextFormat with range";
my_txt.setTextFormat(my_fmt,0,3);
// my_txt.text = "this is for setTextFormat without range";
// my_txt.setTextFormat(my_fmt);
// my_txt.defaultTextFormat = my_fmt;
// my_txt.text = "this is for default text format";
addChild(my_txt);
V.
setTextFormat allows you to change the formatting on parts of the text. Check out the other two parameters of setTextFormat. When you set defaultTextFormat it is applied to all the text that you add to the TextField.
More info :
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#defaultTextFormat
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html#setTextFormat
Edit :
Also setTextFormat doesn’t work on text that is inserted after you set the format. Adobe says
“Any text inserted manually by the user, or replaced by the replaceSelectedText() method, receives the default text field formatting for new text, and not the formatting specified for the text insertion point. To set the default formatting for new text, use defaultTextFormat.”