We are taught that the difference between the defaultTextFormat
property and setTextFormat
method, is that setTextFormat is only used to change text already in the textfield (with an optional character range specified), while defaultTextFormat is what any
new text that is added to the textField will use. There is one
gotcha however, and that is if you change the
last character of a textfield with setTextFormat, defaultTextFormat will no longer be used.
ActionScript Code:
var textField:TextField = new TextField();
textField.width = 500;
addChild(textField);
textField.text = "small LARGE ";
var defaultTextFormat:TextFormat = new TextFormat();
var adjustedTextFormat:TextFormat = new TextFormat();
defaultTextFormat.size = 10;
adjustedTextFormat.size = 20;
textField.defaultTextFormat = defaultTextFormat;
textField.setTextFormat(adjustedTextFormat, 5, 11);// works as expected
//textField.setTextFormat(adjustedTextFormat, 5, 12);// gotcha!
textField.appendText(" new text");
What you get with the first version is:
small LARGE new text
What you get with the second version is:
small LARGE new text
All I changed was the endIndex from 11 to 12, and now the textField.appendText will no longer work as advertised. If you are typing in a wordprocessor and you change the font of the last character, this outcome is expected. Having your wordprocessor remember your last font choice is helpful. When I was coding this however, I did not expect this edge case scenario to perform this way.
Something to keep in mind as you are trying to learn the difference between setTextFormat, and defaultTextFormat. Adobe's definition's hold true, until you tamper with the last character.