如何使用as3将文本与同一行的右边缘和左边缘对齐?

时间:2014-03-11 09:15:46

标签: actionscript-3 flash

是否可以在as3中对齐文本,以便在textField中显示结果?

firstDatasetElement1                                   secondDatasetElement1
firstDatasetElement2                                   secondDatasetElement2
firstDatasetElement3                                   secondDatasetElement3
         .                                                       .
         .                                                       .
         .                                                       .
firstDatasetElementN                                   secondDatasetElementN

2 个答案:

答案 0 :(得分:2)

您可以借助多个文本字段将文本放在多个列中。

var firstCollection:Array = ["First1", "First2", "First3", "First4", "First5", "First6"];
var secondCollection:Array = ["Second1", "Second2", "Second3", "Second4", "Second5", "Second6", "Second7"];

placeTextsAt(this, firstCollection);
placeTextsAt(this, secondCollection, stage.stageWidth);

//Helper function, align text fields to the right size, if width is specified
//holder - container for text, list - your texts, width - if you want align text to the rigth side
function placeTextsAt(holder:DisplayObjectContainer, list:Array, width:uint = 0):void {
    var i:uint, len:uint = list.length, posY:uint, padding:uint = 10, textField:TextField, textFormat: TextFormat = new TextFormat("Arial", 16);

    for (i; i < len; ++i) {
        textField = new TextField();
        textField.defaultTextFormat = textFormat;
        textField.autoSize = TextFieldAutoSize.LEFT;
        holder.addChild(textField);
        textField.text = list[i];
        textField.y = posY;
        posY += (textField.height + padding);

        //Align
        if(width > 0){
            textField.x = width - textField.width;
        }
    }

}

结果你将拥有:

Columns

答案 1 :(得分:0)

我使用单个html文本字段找到了解决方案!它使用&#34;领先&#34; css属性去除&#34; squish&#34;内的多行之间的间距。我创建的标签。

import flash.text.StyleSheet;
var textStyle:StyleSheet = new StyleSheet();

textStyle.setStyle("right", {
    textAlign: "right"
});

textStyle.setStyle("squish", {
    leading: -20
});

myTextBox.styleSheet = textStyle;


myTextBox.htmlText  = "<squish>Stuff on the left!";
myTextBox.htmlText += "\n<right>Stuff on the right!</right></squish>";