/controls/Label.as
ActionScript | 383 lines | 207 code | 18 blank | 158 comment | 23 complexity | 6a47a0ee27c584c655769ee6b85beb78 MD5 | raw file
- package slw.controls
- {
- import flash.display.DisplayObject;
- import flash.display.Loader;
- import flash.display.LoaderInfo;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.net.URLRequest;
- import flash.system.ApplicationDomain;
- import flash.system.LoaderContext;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- import flash.utils.getDefinitionByName;
-
- import slw.core.UIComponent;
-
- /**
- * Label
- * ?????????????????
- * @author ddx<br/>
- * 2011-7-18
- *
- */
- public class Label extends UIComponent
- {
- /**
- * "top"??????
- */
- public static const TOP:String="top";
- /**
- * "bottom"??????
- */
- public static const BOTTOM:String="bottom";
- /**
- * "left"??????
- */
- public static const LEFT:String="left";
- /**
- * "right"??????
- */
- public static const RIGHT:String="right";
- //????
- private var _text:String=null;
- //???????
- private var _gap:Number=5;
- //??
- private var _textField:TextField;
- //??????
- private var _icon:DisplayObject
- //????????
- private var _iconStr:String=null;
- //????
- private var _iconURL:String=null;
- //????
- private var _position:String="";
- //????
- private var _format:TextFormat;
- //??????
- private var _defaultFormat:TextFormat;
- //????
- private var _textColor:uint=0x0;
- /**
- * ????
- * @param text:String="Label" ????
- * @param icon*=null ????
- * @param position:String="left" ??????
- *
- */
- public function Label(text:String="Label",icon:*=null,position:String=LEFT)
- {
- super();
- this.listName="Label";
-
- init();
-
- setProperties(text,icon,position);
- }
-
- /*
- -----------------------------------
- setters getters
- -----------------------------------
- */
- //
- /**
- * ????????TextField.defaultTextFormat
- * @param value:TextFormat
- *
- */
- public function set defaultTextFormat(value:TextFormat):void{
- if(_defaultFormat==value){
- return;
- }
- _defaultFormat=value;
- if(_textField!=null)
- _textField.defaultTextFormat=_defaultFormat;
- _format=null;
-
- updateDisplayList();
- }
- //
- /**
- * ????????TextField.defaultTextFormat
- * @param value:TextFormat
- *
- */
- public function get defaultTextFormat():TextFormat{
- return _defaultFormat;
- }
- //
- /**
- * ???? ???:0x0
- * @param value:uint
- *
- */
- public function set textColor(value:uint):void{
- if(_textColor==value){
- return;
- }
- _textColor=value;
- if(_textField!=null)
- _textField.textColor=_textColor;
- }
- //
- /**
- * ???? ???:0x0
- * @param value:uint
- *
- */
- public function get textColor():uint{
- return _textColor;
- }
- //
- /**
- * ????,???:"Label"?????""?null???????
- * @param value:String
- *
- */
- public function set text(value:String):void{
- if(_text==value){
- return;
- }
- _text=value;
- if(_text==""||_text==null){
- if(_textField!=null)
- this.buildContainer.removeChild(_textField);
- _textField=null;
- }else if(_textField==null){
- _textField=new TextField();
- _textField.autoSize=TextFieldAutoSize.LEFT;
- _textField.textColor=_textColor;
- if(_defaultFormat!=null)
- _textField.defaultTextFormat=_defaultFormat;
- this.buildContainer.addChild(_textField);
- }
-
- updateDisplayList();
- }
- //
- /**
- * ????,???:"Label"?????""?null???????
- * @param value:String
- *
- */
- public function get text():String{
- return _text;
- }
- //
- /**
- * ????????????????????????null?????,???:null
- * @param value:*
- *
- */
- public function set icon(value:*):void{
- if(_icon==value||_iconStr==value){
- return;
- }
- if(_icon!=null)
- this.buildContainer.removeChild(_icon);
-
- _icon=null;
- _iconStr=null;
- if(value is String){
- _iconStr=value;
-
- var $c:Class=getDefinitionByName(_iconStr) as Class;
- _icon=new $c();
- }else{
- _icon=value;
- }
- if(_icon!=null){
- this.buildContainer.addChild(_icon);
- }else{
- _iconURL=null;
- }
-
- updateDisplayList();
- }
- //
- /**
- * ????????????????????????null?????,???:null
- * @param value:*
- *
- */
- public function get icon():*{
- return _icon==null?_iconStr:_icon;
- }
- //
- /**
- * ??URL?????????icon???????????????(?Loader??,??Loader.content??)
- * @param value:String
- * @see <br/>
- * <a href='http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#contentLoaderInfo' target='_blank'>Loader.content</a>
- *
- */
- public function set iconURL(value:String):void{
- if(_iconURL==value){
- return;
- }
- _iconURL=value;
-
- var $context:LoaderContext=new LoaderContext();
- $context.applicationDomain=ApplicationDomain.currentDomain;
- var $loader:Loader=new Loader();
- $loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
- $loader.load(new URLRequest(_iconURL),$context);
- }
- //
- /**
- * ??URL?????????icon???????????????(?Loader??,??Loader.content??)
- * @param value:String
- * @see <br/>
- * <a href='http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#contentLoaderInfo' target='_blank'>Loader.content</a>
- *
- */
- public function get iconURL():String{
- return _iconURL;
- }
- //
- /**
- * ?????????:Label.LEFT,?????:<br/>
- * <ul>
- * <li>Label.TOP ????????<br/>
- * <li>Label.BOTTOM ????????<br/>
- * <li>Label.LEFT ????????<br/>
- * <li>Label.RIGHT ????????<br/>
- * </ul>
- * @param value:String
- *
- */
- public function set position(value:String):void{
- if(_position==value){
- return;
- }
- _position=value;
-
- updateDisplayList();
- }
- //
- /**
- * ?????????:Label.LEFT,?????:<br/>
- * <ul>
- * <li>Label.TOP ????????<br/>
- * <li>Label.BOTTOM ????????<br/>
- * <li>Label.LEFT ????????<br/>
- * <li>Label.RIGHT ????????<br/>
- * </ul>
- * @param value:String
- *
- */
- public function get position():String{
- return _position;
- }
- //
- /**
- * ???????????????? 0
- * @param value:Number
- *
- */
- override public function set round(value:Number):void{
-
- }
- /*
- -----------------------------------
- public methods
- -----------------------------------
- */
- //
- /**
- * ????????
- * @param format:TextFormat
- *
- */
- public function setTextFormat(format:TextFormat):void{
- _format=format;
-
- updateDisplayList();
- }
- /*
- -----------------------------------
- private methods
- -----------------------------------
- */
- //???
- private function init():void{
- this.mouseChildren=false;
- this.mouseEnabled=false;
- }
- //????
- private function updateDisplayList():void{
- if(_icon!=null){
- _icon.x=0;
- _icon.y=0;
- }
-
- if(_textField!=null){
- _textField.text=_text==null?"":_text;
-
- if(_format!=null)
- _textField.setTextFormat(_format);
- _textField.x=0;
- _textField.y=0;
- }
- if(_icon!=null&&_textField!=null){
- switch(_position){
- case TOP:
- _textField.y=_icon.y+_icon.height+_gap;
- layoutX(_textField,_icon);
- break;
- case BOTTOM:
- _icon.y=_textField.y+_textField.height+_gap;
- layoutX(_textField,_icon);
- break;
- case LEFT:
- _textField.x=_icon.x+_icon.width+_gap;
- layoutY(_textField,_icon);
- break;
- case RIGHT:
- _icon.x=_textField.x+_textField.width+_gap;
- layoutY(_textField,_icon);
- break;
- }
- }
- checkoutSize();
- }
- //??????
- private function setProperties(text:String,icon:*,position:String):void{
- _position=position;
- this.text=text;
-
- this.icon=icon;
-
- updateDisplayList();
- }
- //x???
- private function layoutX(...args):void{
- for(var i:int=0;i<args.length;i++){
- if(args[i].width<this.buildContainer.width){
- args[i].x=(this.buildContainer.width-args[i].width)/2;
- }
- }
- }
- //y???
- private function layoutY(...args):void{
- for(var i:int=0;i<args.length;i++){
- if(args[i].height<this.buildContainer.height){
- args[i].y=(this.buildContainer.height-args[i].height)/2;
- }
- }
- }
- //??????
- private function loaderComplete(e:Event):void{
- e.target.removeEventListener(Event.COMPLETE,loaderComplete);
- this.icon=LoaderInfo(e.target).content;
- }
- //????
- private function checkoutSize():void{
- this.width=this.buildContainer.width;
- this.height=this.buildContainer.height;
- }
- }
- }