文本居中显示的四种方法

1. Text.autoSize
      这种方法可以设置文本的对齐方法,按后计算文字长度再通过设置文本的x坐标,从而达到居中显示的目的。
      但这种方法无法在不自动换行的情况下限制文本的长度。也就是说指定txt.width属性是无效的。

  1. myText.autoSize=TextFieldAutoSize.LEFT;
  2. myText.x=stage.stageWidth/2-myText.textWidth/2;

2. TextFormat.align = “center”

这种方法是通过指定一个TextFormat对象给文本的txt.defaultTextFormat属性。

并设置这个TextFormat对象的TextFormat.align = “center”。

这种方法需要设置文本长度,文字将在指定的文本长度中处于居中位置。

  1. var tf:TextFormat = new TextFormat ();
     
  2. tf.align = "center";
     
  3. myText.width = 400;
     
  4. myText.defaultTextFormat = tf;

3. htmlText的p标签

这种方法是在文本的txt.htmlText属性值得中设置对齐方式。

flash中支持的html标签中,p标签支持align属性。

这种方法需要设置文本长度,文字将在指定的文本长度中处于居中位置。

  1. myText.width = 400;
     
  2. myText.htmlText = "<p align='center'>文本内容…</p>";
     

4. styleSheet的textAlign = “center”

这种方法是设置文本的txt.styleSheet,在styleSheet中设置textAlign = “center”。

种方法需要设置文本长度,文字将在指定的文本长度中处于居中位置。

  1. var style:StyleSheet = new StyleSheet();
     
  2. var p:Object = new Object();
     
  3. p.textAlign = "center";
     
  4. style.setStyle("p", p);
     
  5. myText.width = 400;
     
  6. myText.styleSheet = style;
posted on 2011-02-14 09:30  602147629  阅读(2548)  评论(0编辑  收藏  举报