AS3 TextField 自动滚动到底部 [英] AS3 TextField autoscroll to the bottom

查看:26
本文介绍了AS3 TextField 自动滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在以编程方式添加文本的同时自动滚动到 ActionScript 中 TextField 的底部:

How to autoscroll to the bottom of the TextField in ActionScript while adding text there programmatically:

var _output:TextField = new TextField();
for (var i:int = 0; i < 100; ++i) {
    _output.appendText("Hello World!");
}

还要考虑应该启用TextField的垂直滚动,一旦添加了新文本,则应再次执行自动滚动到底部.

Also consider that the vertical scrolling of the TextField should be enabled, and once a new text was added then autoscroll to the bottom should be executed again.

推荐答案

您应该在有问题的 TextField 上监听 Event.CHANGE 事件.与 textField 相关的事件描述 如果您捕获此事件,您将使用 scrollV 属性.像这样说:

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;
}

更新:捕获 Event.CHANGE 不起作用,我保留它以防有人偶然发现此方法并发现它不起作用.因此,唯一的方法是将 TextField 子类化并手动覆盖 appendText() 方法以包含滚动,如下所示:

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; } 
}

这篇关于AS3 TextField 自动滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆