PlaceholderTextField
/**
* Copyright akisute ( http://wonderfl.net/user/akisute )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fke1
*/
package {
import flash.text.TextFormat;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
var tf:PlaceholderTextField = new PlaceholderTextField("Enter something you like...");
var format:TextFormat = new TextFormat();
format.size = 24;
tf.defaultTextFormat = format;
tf.setTextFormat(format);
tf.x = 50;
tf.y = 100;
tf.width = 350;
tf.height = 64;
tf.border = true;
tf.background = true;
this.addChild(tf);
}
}
}
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.FocusEvent;
class PlaceholderTextField extends TextField {
public var placeholder:String;
public var placeholderColor:uint;
private var defaultTextColor:uint;
public function PlaceholderTextField(placeholder:String="", placeholderColor:uint=0x999999) {
trace("call before super();");
this.placeholder=placeholder;
this.placeholderColor=placeholderColor;
super();
trace("call after super();");
this.defaultTextColor=this.textColor;
this.type=TextFieldType.INPUT;
this.addEventListener(FocusEvent.FOCUS_IN, this.onFocusIn);
this.addEventListener(FocusEvent.FOCUS_OUT, this.onFocusOut);
this.showDefaultMessage();
}
public override function set textColor(textColor:uint):void {
super.textColor=textColor;
this.defaultTextColor=textColor;
}
private function onFocusIn(event:FocusEvent):void {
if (this.text==this.placeholder) {
this.hideDefaultMessage();
}
}
private function onFocusOut(event:FocusEvent):void {
if (! this.text) {
this.showDefaultMessage();
}
}
private function showDefaultMessage():void {
this.text=this.placeholder;
super.textColor=this.placeholderColor;
}
private function hideDefaultMessage():void {
this.text="";
super.textColor=this.defaultTextColor;
}
}