4

在我的应用程序中,我想在 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;

它几乎可以正常工作,但返回的 rect 的“x”来源极其“破碎”。例如,如果rect.origin.x开头是 670,最后等于 1022。

有任何想法吗?

谢谢。

4

1 回答 1

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;
}
于 2012-06-19T13:14:17.590 回答