AS3 EaseButton --- 继续自:EaseSprite

package
{
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	/**
	 * 在EasingButton基础上,添加一些属性:text
	 * 添加一些描述效果,自适应文本大小
	 * @author Jave.Lin
	 */
	public class EaseButton extends EaseSprite
	{
		private var _text:String;
		
		public function get text():String{return _text;}
		public function set text(value:String):void
		{
			if(_text!=value)
			{
				_text=value;
				refresh();
			}
		}
		
		private var _textField:TextField;
		
		private var _isAutoSize:Boolean=true;
		
		public function get isAutoSize():Boolean{return _isAutoSize;}
		public function set isAutoSize(value:Boolean):void
		{
			if(_isAutoSize!=value)
			{
				_isAutoSize=value;
				refresh();
			}
		}
		//背景与文字的边框间距
		private var _bgMargin:Number=2;
		
		public function EaseButton($text:String="button")
		{
			this.mouseChildren = false;
			
			text=$text;
			
			filters=[new GlowFilter(0,1,2,2,6)];
		}
		
		private function refresh():void
		{
			if(_textField==null)
			{
				//font Verdana
				_textField=new TextField();
				_textField.defaultTextFormat = new TextFormat("Verdana", 10, 0xffffff);
				_textField.autoSize = TextFieldAutoSize.LEFT;
				_textField.selectable = false;
				//描边
				_textField.filters=[new GlowFilter(0,1,2,2,6)];
				addChild(_textField);
			}
			
			_textField.text=text;
			
			//获取字体位置、大小
			var textFieldRect:Rectangle=_textField.getBounds(this);
			
			//居中
			_textField.x=-textFieldRect.width/2;
			_textField.y=-textFieldRect.height/2;
			
			//背景也居中
			this.graphics.clear();
			this.graphics.beginFill(0x55aa00, 0.5);
			this.graphics.drawRect(
				(-textFieldRect.width/2)-_bgMargin,
				(-textFieldRect.height/2)-_bgMargin,
				textFieldRect.width+(_bgMargin*2),
				textFieldRect.height+(_bgMargin*2)
			);
			this.graphics.endFill();
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值