0

我在运行时创建了一堆 TextField,其中包含htmlText的问题是获取高度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高度?

4

1 回答 1

0

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

我发现如果您将这些测量值用于布局,它们并不完美,如果您将 TextField 缩放到文本内容,通常最好在每个维度上添加 10-20,或者您会截断奇数字符 - 但是这些将返回一个实际值,宽度/高度不会反映尺寸。

希望这可以帮助。

于 2012-10-23T14:59:47.830 回答