首页 文章

如何使TextField在末尾居中?

提问于
浏览
2

我有一个应用程序,其中一些文本一次从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();

我've considered replacing the spaces with a non-space (but still whitespace) character like /u0020, but the font I' m使用似乎不支持 . 当我这样做时,根本没有空格出现(但是 whatToWrite 的痕迹在单词之间返回框) .

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

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

2 回答

  • 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

    更好的方法是创建一个自定义类并使用滑动的完整文本字段进行屏蔽 . 随着文本字段的滑动,您可以扩展自定义类的大小,您将获得更平滑的结果 . 但无论如何,您可以使用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();
        }
    }
    

相关问题