6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSTextView にドラッグ&ドロップしたときのキャレットの位置を取得する

Last updated at Posted at 2014-02-13

NSTextView のキャレットの位置は通常 [aTextView selectedRange].location で取得できる。キャレットの位置は幅0の選択範囲と同義だからである。

しかし、 テキストを選択した状態で NSTextView の上で何かをドラッグしたとき表示されるキャレットの位置はこの方法では取得できない。この場合、 NSDraggingInfo からマウスカーソルの位置を取得し、キャレットの位置に変換すればよい:

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
    NSPoint dropPoint = [self convertPoint:[sender draggingLocation] fromView:nil];
    NSUInteger caretLocation = [self characterIndexForInsertionAtPoint:dropPoint];
}

参考:nstextview - Set the cursor to a pointing hand over a text view - Stack Overflow

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?