objective-c - NSTextView 滚动行为和选择行为

标签 objective-c cocoa nstextview

我有一个 NSTextView,我正在从 NSTask 输出文本。除了滚动和选择行为外,一切都按预期工作。

1:如果我尝试向上滚动,松开后我的滚动位置会立即回到底部。有任何想法吗?我已经查看了很多关于此的文档,但找不到任何相关内容。

2:如果我选择文本,它会删除它。我只想选择它,以便我可以复制和粘贴。也输给了这个。

我们非常欢迎任何提示或指示。谢谢。

- (id)init
{
    [super init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(readPipe:)
                                                 name:NSFileHandleReadCompletionNotification 
                                               object:nil];

    return self;
}

- (void)kicked
{
    task = [[NSTask alloc] init];

    [task setLaunchPath:[self.kickLocationTextField stringValue]];
    [task setArguments:kickBuild];

    NSPipe *pipe = [[NSPipe alloc] init];
    fileHandle = [pipe fileHandleForReading];
    [fileHandle readInBackgroundAndNotify];

    [task setStandardOutput:pipe];
    [task setStandardError:pipe];

    [task launch];

    [task release];
    [pipe release];
}


- (void)readPipe:(NSNotification *)notification
{
    NSData *data;
    NSString *text;

    if( [notification object] != fileHandle )
    {
        return;
    }

    data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
    text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    [nsTaskOutput insertText:text];

    [text release];
    if (data != 0)
    {
        [fileHandle readInBackgroundAndNotify];
    }
}

最佳答案

试试这个而不是 insertText::

NSScroller *scroller = nTaskOutput.enclosingScrollView.verticalScroller;
BOOL shouldScrollToBottom = scroller.doubleValue == 1.0;
NSTextStorage *ts = nTaskOutput.textStorage;
[ts replaceCharactersInRange:NSMakeRange(ts.length, 0) withString:text];
if (shouldScrollToBottom) {
    NSRect bounds = nTaskOutput.bounds;
    [nTaskOutput scrollPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
}

关于objective-c - NSTextView 滚动行为和选择行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024007/

相关文章:

ios - 如果我插入或删除行,UITableView 部分标题会消失

ios - 维护多个 Storyboard的导航栏?

macos - IBOutlet 何时初始化?

ios - NSTask 不适用于具有特殊字符的文件路径

cocoa - NSTextField 和 NSTextView : Overlapping Delegate Methods

macos - 如何全局更改 NSCursor?

ios - 将计算属性从类别转换为 Swift 扩展

objective-c - 你大概花了多长时间学习 Objective-C?

objective-c - Cocoa,如何在 NSTextView 中选择文本

objective-c - 无法在 Objective-C/C 中正确 malloc