AS3 TextField autoscroll to the bottom

可紊 提交于 2019-11-30 14:48:28

You should listen for Event.CHANGE event on the TextField in question. Event description relative to textField And if you capture this event, you play with scrollV property. Say, like this:

_output.addEventListener(Event.CHANGE,scrollAllDown);
function scrollAllDown(e:Event):void {
    var tf:TextField=(e.target as TextField);
    if (!tf) return; 
    tf.scrollV=tf.maxScrollV;
}

Update: Catching Event.CHANGE does not work, I leave this in case someone stumbles on this method and too finds out it doesn't work. So, the only way is to subclass the TextField and manually override appendText() method to include scrolling, like this:

public class OutputTF extends TextField 
{ 
    // constructor omitted
    override public function appendText(text:String):void 
    { super.appendText(text); this.scrollV=this.maxScrollV; } 
}

You can use the scrollV and maxScrollV properties of TextField:

var _output:TextField = new TextField();
for (var i:int = 0; i < 100; ++i) {
    _output.appendText("Hello World!");
    //set vertical scroll position to max value
    _output.scrollV = _output.maxScrollV;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!