NSPoint和IMKCandidate窗口放置

时间:2012-05-18 09:36:51

标签: objective-c pointers input-method-kit

我正在使用inputmethodkit并尝试在某些文本下面放置一个窗口。

_currentClient是IMKTextInput实例

候选人是IMKCandidates个实例

// Get the current location to place the window
NSRect tempRect = NSMakeRect(0, 0, 0, 0);
NSDictionary* clientData = [_currentClient attributesForCharacterIndex:0 lineHeightRectangle:&tempRect];
NSPoint* windowInsertionPoint = (NSPoint*)[clientData objectForKey:@"IMKBaseline"];
...
[candidates setCandidateFrameTopLeft:*windowInsertionPoint];
[candidates showCandidates];

现在,我知道windowInsertionPoint变量很好,当我调试时我可以看到它的值,例如:NSPoint:{647,365}

然而,当我使用它时,候选窗口只显示在屏幕的左下角。我以前没有使用屏幕放置的东西,所以请帮助。

如果我将任意静态值传递给setCandidateFrameTopLeft,它将被放置在屏幕中。以下作品:

[candidates setCandidateFrameTopLeft:NSMakePoint(401, 354)];

这是指针问题吗?

2 个答案:

答案 0 :(得分:1)

好吧,解决这个问题的方法是我是个白痴。以下是您需要的代码:

NSRect tempRect;
NSDictionary* clientData = [_currentClient attributesForCharacterIndex:0 lineHeightRectangle:&tempRect];
NSPoint windowInsertionPoint = NSMakePoint(NSMinX(tempRect), NSMinY(tempRect));

IMKTextInput attributesForCharacterIndex的文档说

  

lineRect:返回时,一个矩形,用线条的高度构成一个像素宽的矩形。该矩形的方向与线的方向相同。

这意味着它将NSRect返回到您为lineHeightRectangle值传递的变量中。重要的一点是,NSRect的位置是您要搜索的角色的位置。那么,你需要从该矩形中指出一个点并使用NSMinY作为Y值。矩形只有一个像素宽,因此X的Min / Max基本相同。

答案 1 :(得分:0)

你可能不再有这个问题了,但这也适用于未来:

            [candidates show:kIMKLocateCandidatesBelowHint];