package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
import flash.text.TextFieldType;
public class TextField_setTextFormatExample extends Sprite {
private var myTextField:TextField = new TextField();
private var newFormat:TextFormat = new TextFormat();
public function TextField_setTextFormatExample() {
myTextField.autoSize = TextFieldAutoSize.LEFT;
myTextField.selectable = true;
myTextField.background = true;
myTextField.type = TextFieldType.INPUT;
myTextField.text = "No matter where you click on this text field only the TEXT IN ALL CAPS changes format.";
myTextField.addEventListener(MouseEvent.CLICK, clickHandler);
newFormat.color = 0xFF0000;
newFormat.size = 18;
newFormat.underline = true;
newFormat.italic = true;
this.addChild(myTextField);
}
private function clickHandler(event:MouseEvent):void {
var tf:TextField = TextField(event.target);
var currentTextFormat:TextFormat = myTextField.getTextFormat(55);
if(currentTextFormat.size != 18) {
myTextField.setTextFormat(newFormat,tf.selectionBeginIndex, tf.selectionEndIndex);
}
else {
myTextField.setTextFormat(myTextField.defaultTextFormat);
}
}
}
}