自动滚动到NSTextView的末尾

自动滚动到NSTextView的末尾

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

问题描述

我有一个NSTextView,我要在上面附加控制台日志.我已经在带有滚动视图的XIB文件中实现了此UI.当我的应用程序在文本视图中追加控制台日志时,我希望滚动条自动转到文本视图的末尾.我在XIB编辑器中找不到任何属性.知道我该如何实现吗?

I have a NSTextView on which I am appending console logs. I have implemented this UI in XIB file with scroll views. When my application appends the console logs in the text view, I want the scroll bar to automatically go to the end of the textview. I could not find any property in XIB editor. Any idea how can I achieve this?

@property(nonatomic, strong) IBOutlet NSTextView *logTextView;

    [[self.logTextView textStorage] beginEditing];
    [[[self.logTextView textStorage] mutableString] appendString:iData];
    [[[self.logTextView textStorage] mutableString] appendString:@"\n"];
    [[self.logTextView textStorage] endEditing];

推荐答案

您可以在附加字符串后使用-[NSText scrollRangeToVisible:]来实现您的目标. (NSTextViewNSText的子类.)

You can achieve your goal by using -[NSText scrollRangeToVisible:] after appending your string. (NSTextView is a subclass of NSText.)

对不起,但这是唯一的方法.如您所见,没有IB复选框会影响此特定行为.

Sorry, but that's the only way. As you observed, there's no IB check box that effects this particular behavior.

一无所有,您可以将两个-[NSMutableString appendString:]调用替换为一个-[NSMutableString appendFormat:]调用.

Apropos of nothing, you could replace your two -[NSMutableString appendString:] calls with one -[NSMutableString appendFormat:] call.

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

09-05 17:52