附加到NSTextView并滚动 [英] Append to NSTextView and scroll

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

问题描述

好的,应该非常简单.但是,我无所不在,我不确定是否找到了100%有效的方法(过去也困扰着我).

OK, what I need should have been very simple. However, I've looked everywhere and I'm not sure I've found something that works 100% (and it's something that has troubled me in the past too).

所以,我们在这里:

  • 我希望能够附加 NSTextView
  • 附加后, NSTextView 应该向下滚动(以便可见最新附加的内容)
  • I want to be able to append to an NSTextView
  • After appending, the NSTextView should scroll down (so that that latest appended contents are visible)

相当简单,是吧?

那么...有什么想法吗?(一个完全执行此简单技巧"的代码示例将超出理想水平……)

So... any ideas? (A code example that performs exactly this simple "trick" would be more than ideal...)

推荐答案

在交叉引用了多个答案和资料(经过一些调整)之后,这是起作用的答案(给出了 _myTextView NSTextView 插座):

After cross-referencing several answers and sources (with some tweaks), here's the answer that does work (given _myTextView is an NSTextView outlet) :

- (void)appendToMyTextView:(NSString*)text
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSAttributedString* attr = [[NSAttributedString alloc] initWithString:text];

        [[_myTextView textStorage] appendAttributedString:attr];
        [_myTextView scrollRangeToVisible:NSMakeRange([[_myTextView string] length], 0)];
    });
}

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

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