如何使TextField在末尾居中?

时间:2016-01-05 16:17:40

标签: actionscript-3 flash animation textfield

我有一个应用程序,其中一些文本一次从TextField的中心扩展一个字母。只要没有空格,它就可以正常工作,但只要字符串中有空格,就会被忽略,直到达到非空格为止,此时文本将自身置于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();

我考虑用/ u0020这样的非空格(但仍然是空白)字符替换空格,但我使用的字体似乎不支持。当我这样做时,根本没有空格出现(但是whatToWrite的痕迹会在单词之间返回框。)

Flash IDE不会考虑最后的空格来居中文本。 myText位于Flash IDE中,未在代码中初始化。

我可以做些什么来完成这项工作?

2 个答案:

答案 0 :(得分:2)

也许有一种更好的方式(如果有人找到更好的方法会很棒),但就目前而言,这是我提出的解决方案/黑客:(仅限单行)

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();

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

答案 1 :(得分:1)

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

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();
    }
}