AS2:获取TextField的真实高度

时间:2012-10-23 12:58:12

标签: flash actionscript-2 textfield

我在运行时创建了一堆包含htmlText的TextFields,问题是获取TextField的高度非常不准确。

如果我访问TextField._heightTextField.height属性,它甚至不接近准确,并且可以在0到1.5行高之间变化。

我试图将TextField放入一个空的MovieClip并获取MovieClip的高度但是会返回相同的不准确度。 (如示例所示)

相关代码:

var format = new TextFormat();
format.size = 14;
format.align = "left";
format.font = "Arial";
format.color = 0x000000;
format.leading = 3;

var textBoxes:Array = new Array();

function createText(htmlString:String, isOption:Boolean, name:String) {
    var textY:Number;
    if(isOption) {
        var textMC:MovieClip = target.attachMovie("option", "txt_" + textBoxes.length, target.getNextHighestDepth());
        textMC.name.text = name;
    } else {
        var textMC:MovieClip = target.createEmptyMovieClip("txt_" + textBoxes.length, target.getNextHighestDepth());
    }
    var fieldContainer:MovieClip = textMC.createEmptyMovieClip("container", textMC.getNextHighestDepth());
    var textBox:TextField = fieldContainer.createTextField("textBox", fieldContainer.getNextHighestDepth(), studySettings.margin, studySettings.margin, target._width - (studySettings.margin * 2),0);
    textBox.autoSize = true;
    textBox.wordWrap = true;
    textBox.html = true;
    textBox.multiline = true;
    textBox.htmlText = htmlString;
    textBox.setTextFormat(format);
    textBox.embedFonts = true;
    textBox.selectable = false;
    textBox.antiAliasType = "advanced";
    if(isOption) {
        textMC.option = currentOption;
        textMC.enabled = false;
        textBox._y += textMC.background._y;
        textBox._width = textMC._width - (studySettings.margin * 3);
        textMC.background._width = target._width - (studySettings.margin * 2);
        textMC.background._height = fieldContainer._height + (studySettings.margin * 2);
        textMC._x = studySettings.margin;
        textMC.onRollOver = function() {
            this.background.gotoAndStop("over");
        }
        textMC.onRollOut = function() {
            this.background.gotoAndStop("up");
        }
        textMC.onRelease = function() {
            showStudy(study.caseIndex, study.choiceIndex, false, this.option);
            clearStudy(study, false);
        }
    }
    if(textBoxes.length) {
        var prebounds:Object = textBoxes[textBoxes.length - 1].getBounds(target);
        textMC._y = (isOption) ? prebounds.yMax + studySettings.margin : prebounds.yMax;
    } else {
        textMC._y = 0;
    }
    textBoxes.push(textMC);

如何才能可靠地获得TextField身高?

1 个答案:

答案 0 :(得分:0)

TextField.textWidth / TextField.textHeight应该返回该字段的内容的实际尺寸。

我发现如果你使用这些测量来进行布局它们并不完美,如果你将TextField缩放到文本内容,通常最好将10-20添加到每个维度,或者你得到奇怪的字符切割关闭 - 但这些将返回实际值,宽度/高度不会反映尺寸。

希望这有帮助。