NSTextView滚动行为和选择行为

时间:2011-11-05 23:11:09

标签: 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];
    }
}

1 个答案:

答案 0 :(得分:3)

尝试使用此代替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))];
}