title: How to calculate correct coordinates for selected text in NSTextView?
body: <p>I need to highlight line with caret in <code>NSTextView</code> using <code>CALayer</code> overlay and grab rect for line with this code in my subclassed <code>NSTextView</code>...:</p>
  
  <pre><code>- (NSRect)overlayRectForRange:(NSRange)aRange
  {
      NSScreen *currentScreen = [NSScreen currentScreenForMouseLocation];
      NSRect rect = [self firstRectForCharacterRange:aRange];
  
      rect = [self convertRectToLayer:rect];
      rect.origin = [currentScreen flipPoint:rect.origin];
      rect = [self.window convertRectToScreen:rect];
  
      return rect;
  }
  </code></pre>
  
  <p>...and put overlay:</p>
  
  <pre><code>- (void)focusOnLine
  {
      NSInteger caretLocation = [aTextView selectedRange].location;
      NSRange neededRange;
  
      (void)[layoutMgr lineFragmentRectForGlyphAtIndex:caretLocation effectiveRange:&amp;neededRange];
      CALayer *aLayer = [CALayer layer];
      [aLayer setFrame:NSRectToCGRect([aTextView overlayRectForRange:neededRange])];
      [aLayer setBackgroundColor:[[NSColor redColor] coreGraphicsColorWithAlfa:0.5]];
      [[aTextView layer] addSublayer:aLayer];
  }
  </code></pre>
  
  <p>As a result, the selection overlay coincides with width of desired line, but absolutely not matches by Y axis (X axis is ok).</p>
  
  <p>What am I missing?</p>