Created
December 19, 2009 04:29
-
-
Save akisute/259954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.text.TextField; | |
import flash.text.TextFieldType; | |
import flash.events.FocusEvent; | |
public 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment