AS3文本中文本对齐

在AS3中使文本对其比如说居中对齐,可以使用autoSize=TextFieldAutoSize.CENTER,但是发现如果文本TextField设置了wordWrap = true后,对齐就不好使了,查了一下文档wordWrap的设置回对autoSize属性有影响,其实我们可以使用TextFormat来实现居中显示,测试代码如下:

不设置TextFormat时

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * 
	 * @author aisajiajiao
	 * 
	 */	
	public class TextAlignTest extends Sprite
	{
		private var tf:TextField;
		
		public function TextAlignTest()
		{
			var format:TextFormat = new TextFormat();
			format.align = TextFormatAlign.CENTER;
			
			tf = new TextField();
//			tf.defaultTextFormat = format;
			tf.width = 100;
			tf.text = "asssasasasasasasasasssasasasasasasas";
			tf.border = true;
			tf.wordWrap = true;
			tf.autoSize = TextFieldAutoSize.CENTER;
			addChild(tf);
		}
		
	}
}

截图如下,可以看出他是没有实现我们想要的居中对齐的效果的,


设置TextFormat时,

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * 
	 * @author aisajiajiao
	 * 
	 */	
	public class TextAlignTest extends Sprite
	{
		private var tf:TextField;
		
		public function TextAlignTest()
		{
			var format:TextFormat = new TextFormat();
			format.align = TextFormatAlign.CENTER;
			
			tf = new TextField();
			tf.defaultTextFormat = format;
			tf.width = 100;
			tf.text = "asssasasasasasasasasssasasasasasasas";
			tf.border = true;
			tf.wordWrap = true;
//			tf.autoSize = TextFieldAutoSize.CENTER;
			addChild(tf);
		}
		
	}
}


你可能感兴趣的:(flash)