我在运行时创建了一堆 TextField,其中包含htmlText
的问题是获取高度TextField
是非常不准确的。
如果我访问TextField._height
或TextField.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
高度?