2012-06-19 143 views
4

在我的應用程序中,我想在NSTextView中顯示彈出窗口,並且需要將NSTextView中所選矩形的座標轉換爲屏幕座標。我試過這個代碼:將NSTextView座標轉換爲屏幕座標

NSWindow* viewWindow = [self window]; 
NSRect rect = [self firstRectForCharacterRange:[self selectedRange]]; 
rect = [[self superview] convertRect:rect toView:nil]; //converting to NSClipView coordinate system 
rect = [viewWindow convertRectToScreen:rect]; 
return rect; 

它的工作幾乎不錯,但返回矩形有極其「破」起源由「X」。例如,如果rect.origin.x在開始時是670,最後它等於1022.

任何想法?

謝謝。

回答

4

我明白了。

- (NSRect)rectForPopover 
{  
    NSRect rect = [self firstRectForCharacterRange:[self selectedRange]]; 
    NSRect txtViewBounds = [self convertRectToBacking:[self bounds]]; 
    txtViewBounds = [self.window convertRectToScreen:txtViewBounds]; 

    rect = [[self superview] convertRect:rect toView:nil]; 
    rect = [self.window convertRectToScreen:rect]; 

    rect.origin = NSMakePoint(rect.origin.x - txtViewBounds.origin.x - self.window.frame.origin.x, rect.origin.y); 

    return rect; 
} 
相關問題