如何将末尾有空格的 TextField 居中?

How to center a TextField with spaces at the end?

我有一个应用程序,其中一些文本从 TextField 的中心开始扩展,一次一个字母。只要没有 space 就可以正常工作,但是一旦在字符串中达到 space,它就会被忽略,直到达到非 space,此时将文本本身置于 TextField 中。

myText 是舞台上的 TextField,文本居中作为其默认对齐方式。

// Write the words
var charBetweenWords:String = " ";
var whatToWrite:String = "THERE ARE 200 BARRELS OF OIL IN ONE TANKER TRUCK";
whatToWrite = whatToWrite.split(" ").join(charBetweenWords);
var wordTimer:Timer = new Timer(100, 1);
wordTimer.addEventListener(TimerEvent.TIMER_COMPLETE, wordHandler);
function wordHandler(e:TimerEvent)
{
    if (whatToWrite.length > 0)
    {
        myText.appendText(whatToWrite.substr(0, 1));
        whatToWrite = whatToWrite.substr(1);
        wordTimer = new Timer(5, 1);
        wordTimer.addEventListener(TimerEvent.TIMER_COMPLETE, wordHandler);
        wordTimer.start();
    }
    else
    {
        // Done
    }
}
wordTimer.start();

我考虑过将 spaces 替换为非 space(但仍然是白色space)字符,例如 /u0020,但我使用的字体没有似乎不支持这一点。当我这样做时,根本没有 space 出现(但单词之间有 whatToWrite returns 框的痕迹)。

考虑到 spaces 在末尾,Flash IDE 不会将文本居中。 myText 放在 Flash 中 IDE 并没有在代码中初始化。

我该怎么做才能完成这项工作?

也许有更好的方法(如果有人找到更好的方法那就太好了),但现在,这是 solution/hack 我想出的:(仅单行)

var whatToWrite:String = "THERE ARE 200 BARRELS OF OIL IN ONE TANKER TRUCK";    
var wordTimer:Timer = new Timer(100, 1);
wordTimer.addEventListener(TimerEvent.TIMER, wordHandler);

//set the the auto size to left so the width matches the text width
myText.autoSize = TextFieldAutoSize.LEFT;

function wordHandler(e:TimerEvent)
{
    if (whatToWrite.length > 0){
        //if the new character to add is a space, replace it with an underscore
        var newChar:String = whatToWrite.substr(0, 1);
        var charToAdd = newChar == " " ? "_" : newChar;
        myText.appendText(charToAdd);

        whatToWrite = whatToWrite.substr(1);

        wordTimer.reset();
        wordTimer.delay = 150;
        wordTimer.start();

        //center the text field manually
        myText.x = (stage.stageWidth - myText.width) * 0.5;

        //if it was originally a space, take off the underscore and make it a space again.
        if(newChar == " ") myText.text = myText.text.substring(0,myText.text.length-1) + " ";

    }
    else
    {
        // Done
    }
}

wordTimer.start();

如果您需要多行,则必须以编程方式将每一行拆分到它自己的文本字段中。

更好的方法是创建一个自定义 class 并使用带有滑入的完整文本字段的遮罩。随着文本字段滑入,您可以扩展自定义 class 大小,您将获得更平滑的结果.但是无论如何,您可以使用 TextLineMetrics class 获得非常精确的结果,它提供了有关文本中所有字符的大量信息。更简单的方法是获取字符的边界:

import flash.geom.Rectangle;

var whatToWrite:String = "THERE ARE 200 BARRELS OF OIL IN ONE TANKER TRUCK";
var wordTimer:Timer = new Timer(25);
wordTimer.addEventListener(TimerEvent.TIMER, wordHandler);
wordTimer.start();
var textWidth:Number = 0;
function wordHandler(e:TimerEvent)
{
    if (whatToWrite.length)
    {
        myText.appendText(whatToWrite.substr(0, 1));
        var rect:Rectangle = myText.getCharBoundaries(myText.text.length - 1)       
        textWidth += rect.width;        
        myText.width = textWidth + 5//compensate for rect with + 5
        whatToWrite = whatToWrite.substr(1);
        myText.x = stage.stageWidth / 2 - myText.width / 2
    }
    else
    {
        wordTimer.stop();
    }
}