此代码可用于调整TextField的大小,使其足够大以适合文本而不需要滚动。
var metrics:TextLineMetrics = getLineMetrics(numLines - 1); //get metrics of last line of text
var minimum_height = metrics.height + metrics.leading; //include one extra leading in all textHeight measurements to avoid undercalculating the textHeight (maxScrollV seems to use a value for textHeight that includes an extra leading value)
var real_textHeight:Number = textHeight + metrics.leading; //include one extra leading in all textHeight measurements to avoid undercalculating the textHeight (maxScrollV seems to use a value for textHeight that includes an extra leading value)
var gutter:Number = 4; //2px top and 2px bottom gutter exists on all textfields
height = (h < minimum_height) ? (minimum_height + gutter) : (real_textHeight + gutter); //match height of text, ensuring height is at least enough to display one line, even if there is no text. also accounts for 4 pixel gutter and extra leading value that would otherwise throw off maxScrollV
在你的情况,你是不是要调整文本框,但原则上,以确定是否显示滚动条,你想使用:
var needsScrollBars:Boolean = ((textfield.textHeight + metrics.leading) > (height + 4));
的关键是以包含该额外的领先值,这不包含在textHeight属性中,但似乎被maxScrollV考虑在内。如果前导为零,那么maxScrollV太多的问题就会消失。
如果您的问题是更新字段中的延迟,请确保最后设置“文本”属性。如果您正在执行自动调整大小,则可以更改高度,然后重置textfield.text或调用textfield.getCharBoundaries或textfield.getLineMetrics,这似乎迫使它更新其内部值。
最后,我注意到的另一个问题是numLines最初是错误的。例如,舞台上的空白文本字段将报告numLines = 2.一旦在运行时设置了“文本”,该值就会自行修正,所以如果您只设置了text = ""
,则numLines是正确的。在任何情况下,textHeight + leading对于文本的高度都是一个很好的“真实”值,并且始终“为了避免滚动而需要的文本字段高度”应该是“真实文本高度”+ 4.
是啊,问题是“为什么maxScrollV在不滚动的字段上报告2” – Andrew 2010-12-09 10:21:21
当高度发生变化时,它可能不会立即重新计算这些维度,尽管我认为它在文本更改时会这样做,所以您想要改变最后。或者,如果您调用textfield.getCharBoundaries或textfield.getLineMetrics,它似乎更新它的值。 – Triynko 2013-08-12 16:29:30