私が抱えている問題は、複数の TextField が表示されることを期待しているのに、1 つしか表示されないことです。このコードに絞り込みました。List 要素を追加すると、空の四角形が表示され、その下に予想どおりの正確なサイズの四角形が表示されます。問題は、TextField が 2 番目の四角形にしか表示されないことです。3 つの要素を追加すると、最後の要素のみが視覚化されます。
次のコードは、DisplayObjects (現在は 2 つの TextFields) のリストを取得し、リストを反復処理して、それぞれのコンテナ Sprite を作成します。それぞれのコンテナ Sprite は、各 DisplayObject の高さによってオフセットされ、視覚的なリストを効果的に作成します。リスト内で同じ TextField が 2 回以上使用されている場合、TextField は最後の要素にのみ描画されます。各コンテナ スプライトには、それが存在することを示すバウンディング ボックスが含まれています。
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class List extends Sprite
{
private var data:Array=new Array;
private var label:TextField= new TextField();
public function List()
{
super();
label.selectable=true;
label.autoSize = TextFieldAutoSize.LEFT;
label.antiAliasType = AntiAliasType.ADVANCED;
label.text = "Testing";
data.push(label);
data.push(label);
}
public function renderList():void{
var height:int=0;
for (var i:int=0; i< data.length; i++){
//get current sprite in list
var current:DisplayObject=data[i];
//create new sprite to contain element of array
var listItem:Sprite=new Sprite;
listItem.addChild(current);
//draw bounding rectangle for reference
var rect:Rectangle=current.getBounds(this);
listItem.graphics.lineStyle(1, 0x000000);
listItem.graphics.beginFill(0xFFFFFF);
listItem.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
listItem.graphics.endFill();
//set height corresponding to bounds height
listItem.y=height;
//calculate height for next item
height=height + rect.height;
//add new list item
addChild(listItem);
}
}
}
}