之前遇到这样一个问题,就是要结合TextArea和Text的特性,让一个输入文本框的高度能自动调节,同时还得能保留输入的特性。如果单纯是需要让其可输入的话,用之前的Text.label.type="input"就可以了,但因为Text组件是继承自UIObject的,将无法侦听keyDown、keyUp之类的键盘事件,所以最终的解决方式是扩展TextArea组件。代码很简单:
//TextAreaExt.as
import mx.controls.TextArea;
class scripts.TextAreaExt extends TextArea
{
function TextAreaExt()
{
}
public function measure():Void
{
super.measure();
_measuredPreferredHeight = label.textHeight + 4;
}
function draw():Void
{
super.draw();
invalidateSize();
addEventListener("change", this);
}
function change()
{
invalidateSize();
}
}
即覆盖了measure方法,让高度不再是固定值,而是与显示文字的高度相关。在每次文字内容改变的时候重新计算高度,更新显示。
Flash 动画:
//TextAreaExt.as
import mx.controls.TextArea;
class scripts.TextAreaExt extends TextArea
{
function TextAreaExt()
{
}
public function measure():Void
{
super.measure();
_measuredPreferredHeight = label.textHeight + 4;
}
function draw():Void
{
super.draw();
invalidateSize();
addEventListener("change", this);
}
function change()
{
invalidateSize();
}
}
即覆盖了measure方法,让高度不再是固定值,而是与显示文字的高度相关。在每次文字内容改变的时候重新计算高度,更新显示。
Flash 动画:
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}