私は、この種の動作を達成するためにハックの回避策を見つけました。私はdemo projectを作成しました。関連するクラスはTerminalLikeTextViewです。このソリューションは完璧に機能しますが、私はまだより良いソリューションを望んでいます.Hackyが少なく、NSTextViewの内部機構に依存しないようにしてください。
重要なステップは次のとおりです。
1)を設定しmouseDownFlagにYESにダウンマウスの前とNOの後:
@property (assign, nonatomic) BOOL mouseDownFlag;
- (void)mouseDown:(NSEvent *)theEvent {
self.mouseDownFlag = YES;
[super mouseDown:theEvent];
self.mouseDownFlag = NO;
}
2)updateInsertionPointStateAndRestartTimer
メソッドから早期復帰を更新から挿入ポイントを防ぐために:
- (void)updateInsertionPointStateAndRestartTimer:(BOOL)flag {
if (self.mouseDownFlag) {
return;
}
[super updateInsertionPointStateAndRestartTimer:flag];
}
3)最初の2つのステップではマウスで移動しない挿入ポイントが作成されますが、selectionRange
はまだ変更されますそれを追跡するために、EED:必要に応じて
static const NSUInteger kCursorLocationSnapshotNotExists = NSUIntegerMax;
@property (assign, nonatomic) NSUInteger cursorLocationSnapshot;
#pragma mark - <NSTextViewDelegate>
- (NSRange)textView:(NSTextView *)textView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange {
if (self.mouseDownFlag && self.cursorLocationSnapshot == kCursorLocationSnapshotNotExists) {
self.cursorLocationSnapshot = oldSelectedCharRange.location;
}
return newSelectedCharRange;
}
4)キーを使用して印刷しようとすると、場所を復元します:
- (void)keyDown:(NSEvent *)event {
NSString *characters = event.characters;
[self insertTextToCurrentPosition:characters];
}
- (void)insertTextToCurrentPosition:(NSString *)text {
if (self.cursorLocationSnapshot != kCursorLocationSnapshotNotExists) {
self.selectedRange = NSMakeRange(self.cursorLocationSnapshot, 0);
self.cursorLocationSnapshot = kCursorLocationSnapshotNotExists;
}
[self insertText:text replacementRange:NSMakeRange(self.selectedRange.location, 0)];
}
は編集= NO、選択= YES十分だろうか?あなたは何か違うものが必要ですか?あなたは明確にすることができますか? – Remizorrr
'insertText:replacementRange:'は 'editable = NO'で動作しますが、テキストを追加するわけではありません。 –
@StanislavPankevichは、貼り付け直前にYESに編集可能に設定してから、NOに戻すことができます。 – faviomob