In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

PlaceholderTextField

Get Adobe Flash player
by akisute 19 Dec 2009

    Talk

    akisute at 19 Dec 2009 08:41
    とりあえずバージョン1 Known Problems ============= * textFormat is canceled on click/edit ** -> Maybe defaultTextFormat may help? try out
    akisute at 19 Dec 2009 08:42
    バージョン2 Change Note ========== * fixed the bug where textFormat is canceled on click/edit ** -> defaultTextFormat could solve this
    akisute at 19 Dec 2009 08:44
    call before super();だのcall after super();だのはsuper()呼び出しの前に別のステートメントが普通に書けてびっくりしているJava使いの悲しい性なので特に深い意味はありません むしろ邪魔なので消しておk

    Tags

    Embed
/**
 * 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;
	}
}