flash - ActionScript 的 WordWrap 打破单词

标签 flash actionscript-3 actionscript word-wrap

我有一个具有设定高度和宽度的文本字段,并且允许自动换行。当我向其中添加文本时,我会动态更改字体大小,直到文本的高度小于框的高度,使其能够正确适合。我遇到的问题是,盒子必然相当薄,并且一些较长的单词(例如“澳大利亚”)在自动换行期间被破坏。因此,也许“Australi”会出现在第一行,“a”会出现在第二行。然而,因为它们满足了高度要求,所以它们不再缩小。我需要一种方法来检测这一点并继续缩小这些案例,以便它们能够顺利贴合。以下是动态缩小文本以适应高度的代码:

text.wordWrap = true;
text.autoSize = TextFieldAutoSize.CENTER;

while(text.height > textBoxVal) {
    myFormat.size = Number(myFormat.size) - 2;
    text.defaultTextFormat = myFormat;
    text.setTextFormat(myFormat);
}

这很好用。我的下一步方法的问题(如果由于单词被分割而未满足水平约束则缩小)是,通过将 text.wordWrap 设置为 true,较长的单词仍然会中断并且永远不会可以调整大小,但将其设置为 false 意味着之前由于自动换行而正常的其他框(例如“美国”)现在在单行上太长,并且在不应该收缩时被缩小。对于它的值(value),这是我的水平收缩代码(紧随上面的代码):

text.wordWrap = false;
text.autoSize = TextFieldAutoSize.LEFT;
while(text.width > textBoxVal) {
    myFormat.size = Number(myFormat.size) - 2;
    text.defaultTextFormat = myFormat;
    text.setTextFormat(myFormat);
}

有没有更好的方法来解决这个问题?也许有某种方法可以强制 ActionScript 不要破坏单词等?

编辑:举个例子,请看下面的屏幕截图:

enter image description here

这仅使用第一个代码块,以确保文本适合框的垂直约束。您可以看到一些盒子(例如美国的第一个)很好,而一些较小的盒子(例如最后几个)是分开的。如果自动换行为 true,则使用我的第二个代码块调整水平约束的大小不会发生任何变化,并且如果自动换行设置为 false,则将使所有框(包括很好的框,例如美国)小于所需的大小。

最佳答案

如果您的文本换行,您应该能够 检查 maxScrollV。

我的猜测如下:

while((text.height > textBoxVal || text.maxScrollV > 1) && myFormat.size > 1) {
    myFormat.size = Math.max(1, Number(myFormat.size) - 2);
    text.defaultTextFormat = myFormat;
    text.setTextFormat(myFormat);
}

编辑

我的建议是

  1. 逐字将文本添加到字段中
  2. 如果 maxScrollV 发生变化,您将获得自动换行
  3. 插入隐式换行符
  4. 调整文本大小以适合所需的高度
  5. 重复直到所有单词都不存在

下面是一些代码来说明这一点:

private function fitText (text:String, rect:Rectangle, tf:TextField, format:TextFormat) : void
{
    // split text by whitespace
    var heap:Array = text.split(/\s+/);

    var size:uint = format.size;
    var maxScroll:uint = 1;
    var text:String;
    var word:String;

    // make sure tf is set up correctly
    tf.defaultTextFormat = format;
    tf.setTextFormat(format);
    tf.multiline = true;
    tf.wordWrap = true;
    tf.autoSize = TextFieldAutoSize.LEFT;
    tf.width = rect.width;

    for (var i:uint = 0; i<heap.length;i+=1) {    
        // insert text word by word
        word = heap[i];
        text = tf.text;
        if (i) {
            tf.text = text + " " + word;
        } else {
            tf.text = word;
        }
        if (tf.maxScrollV > maxScroll) {        
            // the last word you inserted increased number of lines 
            // and possibly got broken

            // increase scroll by 1
            // if scroll actually increased by more than 1,
            // we got word broken multiple times
            maxScroll += 1;

            // insert implicit line break, if not the first word
            if (i) {
                tf.text = text + "\n" + word;
            }
        }

        // do the resizing routine, if needed
        while (tf.height > rect.height || tf.maxScrollV > maxScroll) {
            // we also check tf.maxScrollV,
            // as it can be no more than maxScroll.
            // otherwise we'll get words crippled    

            /*resizing code here*/
        }
    }

    // restore original size
    format.size = size;
}

关于flash - ActionScript 的 WordWrap 打破单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521507/

相关文章:

php - 解析 swf/fla(使用 php?)

javascript - 如何从 javascript 检查 flash 对象是否处于全屏模式

xml - 将 XML 节点转换为 MovieClip

javascript - 什么更有效率?检查 == 或只是改变变量?

actionscript-3 - 从字符串创建一个单词数组(字符串)

flash - QWebview:如何检测Flash视频完成?

flash - 圆 Angular 不显示在闪光灯顶部

javascript - 从 Flash 到 Javascript OOP 对象的 ExternalInterface 调用

c++ - 通过 FLASCC 从 C++ 到 Actionscript

ios - 我可以重写附带的 AS1 Flash 游戏并创建具有相同性能的 HTML5/JavaScript/CSS3/PhoneGap iPad 应用程序吗?