ActionScript 3.0 学习(七) AS3 TextFormat及StyleSheet设置文本类TextField字体格式

         运用TextFormat时,将字体格式设置好后,将其赋值给TextField的defaultTextFormat属性。程序如下:     
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	public class TextStudy extends Sprite
	{
		private var _score:int = 0;
		private var _count:int = 0;
		private var _txt:TextField;
		private var _textFormat:TextFormat = new TextFormat();
		public function TextStudy()
		{
			_txt= createText(0, 0, stage.stageWidth, stage.stageHeight);
			_txt.type = TextFieldType.DYNAMIC;
			_txt.text ="" + _count;
			addChild(_txt);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(evt:Event):void
		{
			_count++;
			_txt.text = "得分:" + _count;
		}
		
		private function createText(x:int, y:int, width:int, height:int):TextField
		{
			var result:TextField = new TextField();
			_textFormat.font = "Arial";
			_textFormat.size = 20;
			_textFormat.color = 0xff0000;
			result.defaultTextFormat = _textFormat;
			result.x = x;
			result.y = y;
			result.width = width;
			result.height = height;
			result.background = true;
			result.backgroundColor = 0xf0f0f0;
			return result;
		}
	}
}

运行结果如图所示:

          运用StyleSheet时,将字体格式设置好后,将其赋值给TextField的styleSheet属性。程序如下:


package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.StyleSheet;
	
	public class TextStudy extends Sprite
	{
		private var _xml:XML;
		private var _txt:TextField;
		private var style:StyleSheet;
		public function TextStudy()
		{
			_xml = <xml><fps>FPS:100</fps><ms>MS:50</ms><mem>MEM:20</mem><memMax>MAX:150</memMax></xml>;
			
			style = new StyleSheet();
			style.setStyle('xml', {fontSize:'9px', fontFamily:'_sans', leading:'-2px'});
			style.setStyle('fps', {color: "#ff0000"});
			style.setStyle('ms', {color: "#00ff00"});
			style.setStyle('mem', {color: "#0000ff"});
			style.setStyle('memMax', {color: "#00f0f0"});
			
			_txt = new TextField();
			_txt.x = 100;
			_txt.y = 100;
			_txt.width = 100;
			_txt.height = 100;
			_txt.styleSheet = style;
			_txt.condenseWhite = true;
			_txt.selectable = false;
			_txt.mouseEnabled = false;
			
			_txt.htmlText = _xml;
			
			addChild(_txt);
		}
	}
}


运行结果如图所示:





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页