NSTextView Text Dragging Position

  • Does anyone know how to determine the character positions that text
    was dropped onto an NSTextView? I need to manipulate the string and
    do my own custom insertion but I haven't yet figured out how to find
    the character position to insert the text at.

    Thanks

    --
    Seth Willits
  • on 05.10.14 5:38 AM, Seth Willits at <seth...> wrote:
    > Does anyone know how to determine the character positions that text
    > was dropped onto an NSTextView? I need to manipulate the string and
    > do my own custom insertion but I haven't yet figured out how to find
    > the character position to insert the text at.

    You can get the dragging location from  NSDraggingInfo.
    For example,

    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {

        NSPoint draggingLocation = [sender draggingLocation];
        draggingLocation = [self convertPoint:draggingLocation fromView:nil];
        ......

    }

    Satoshi
    -----------------------------------------------------
    Satoshi Matsumoto
    816-5 Odake, Odawara, Kanagawa, Japan 256-0802
  • on 05.10.14 5:38 AM, Seth Willits at <seth...> wrote:
    > Does anyone know how to determine the character positions that text
    > was dropped onto an NSTextView? I need to manipulate the string and
    > do my own custom insertion but I haven't yet figured out how to find
    > the character position to insert the text at.

    You can get the dragging character location from  NSDraggingInfo.

    For example,

    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {

        NSPoint draggingLocation = [sender draggingLocation];
        draggingLocation = [self convertPoint:draggingLocation fromView:nil];
      unsigned int characterIndex = [self
    characterIndexOfPoint:draggingLocation];
        ......

    }

    - (unsigned int)characterIndexOfPoint:(NSPoint)aPoint
    {
    unsigned int glyphIndex;
    NSLayoutManager *layoutManager = [self layoutManager];
    float fraction;
    NSRange range;

        range = [layoutManager glyphRangeForTextContainer:[self textContainer]];
        glyphIndex = [layoutManager glyphIndexForPoint:aPoint
    inTextContainer:[self textContainer]
    fractionOfDistanceThroughGlyph:&fraction];
        if( fraction > 0.5 ) glyphIndex++;

        if( glyphIndex == NSMaxRange(range) ) return  [[self textStorage]
    length];
        else return [layoutManager characterIndexForGlyphAtIndex:glyphIndex];

    }

    Satoshi

    -----------------------------------------------------
    Satoshi Matsumoto <satoshi...>
    816-5 Odake, Odawara, Kanagawa, Japan 256-0802
  • On Oct 13, 2005, at 6:19 PM, Satoshi Matsumoto wrote:

    > - (unsigned int)characterIndexOfPoint:(NSPoint)aPoint
    > {
    > ...
    > }

    Awesome. I'll test this tomorrow. I'm guessing it works, so in
    advance: thanks!

    --
    Seth Willits
previous month october 2005 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today