如何使动态文本框按下其他控件

时间:2011-12-06 12:20:44

标签: flash actionscript-3 flash-cs4 textfield

这是一个非常容易的,我认为很容易在谷歌上找到,但我想不出这个术语。

我正在使用CS4和AS3以及一些相互之间的多行动态文本框。当我填充顶部文本框时,我希望当内容流向额外的行时,它会自动按下它下面的其他文本框。

目前它只包裹在每个文本框之间的间隙,但在到达下面的文本框时停止。

是否有允许这样做的属性(我在Silverlight中工作并且有控件存在)我可以设置或者我必须手动实现它并调用功能来重新设置所有控件y属性每次我在文本框中更改文本?

由于

2 个答案:

答案 0 :(得分:1)

首先需要自动调整文本字段

myTextField.autoSize = TextFieldAutoSize.LEFT

然后您需要收听文本字段更改或用户输入

myTextField.addEventListener(Event.CHANGE, function(e:Event):void{
    var newHeight:number = myTextField.height;
}

根据新的高度,您可以向上或向下移动其下的其他元素

答案 1 :(得分:1)

我不知道有任何属性可以轻松地做到这一点,但我手动实现了类似的东西。每当你想调整textFields时,都要调用类似这样的函数: (未经测试的代码)

 private function arrangeText():void {

    var maxWidth:Number = 400; //the ending width of your

    //Set width to something low for all textfields, 
    //to get accurate reading of textWidth
    txtTitle.width = 200; 
    txtSub.width = 200;

    //Set autosizing temporarily
    txtTitle.autoSize = TextFieldAutoSize.LEFT;
    txtSub.autoSize = TextFieldAutoSize.LEFT;

    //Get roughly the number of lines required to display all the text
    var titleLines:Number = Math.ceil(txtTitle.textWidth / maxWidth) - 1;
    var subLines:Number = Math.ceil(txtSub.textWidth / maxWidth) - 1;

    //Now that you know roughtly how many lines are needed, 
    //Take off autosizing
    txtTitle.autoSize = TextFieldAutoSize.NONE;
    txtSub.autoSize = TextFieldAutoSize.NONE;

    //set textfield widths bask to normal
    txtTitle.width = maxWidth;
    txtSub.width = maxWidth;

    //Apply new height values to textfields
    //Prevent textfields from being too tall as well with the "> 3" part
    txtTitle.height = (titleLines > 3 ? 100 : txtTitle.textHeight) + 5;
    txtSub.height = (subLines > 3 ? 60 : txtSub.textHeight) + 5;

    //Starting from the top going down
    //Move each of your textfields under the previous one
    txtSub.y = txtTitle.x + txtTitle.height;

}