Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 将NSTextView坐标转换为屏幕坐标_Cocoa_Nstextview_Nspopover - Fatal编程技术网

Cocoa 将NSTextView坐标转换为屏幕坐标

Cocoa 将NSTextView坐标转换为屏幕坐标,cocoa,nstextview,nspopover,Cocoa,Nstextview,Nspopover,在我的应用程序中,我想在NSTextView中显示popover,并需要将NSTextView中所选矩形的坐标转换为屏幕坐标。我尝试过以下代码: NSWindow* viewWindow = [self window]; NSRect rect = [self firstRectForCharacterRange:[self selectedRange]]; rect = [[self superview] convertRect:rect toView:nil]; //converting t

在我的应用程序中,我想在NSTextView中显示popover,并需要将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;
它工作得几乎很好,但返回的rect已被“x”极大地“破坏”了原点。例如,如果开始时
rect.origin.x
为670,则最后等于1022

有什么想法吗

谢谢。

我知道了

- (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;
}