NSTextview:如何获取插入点的字形矩形?自定义TextView?

时间:2013-05-05 17:43:43

标签: objective-c xcode macos cocoa nstextview

我似乎无法找到有关如何获取插入点的矩形的任何文档。我正在尝试在文本插入符/插入点

的正下方显示一个视图(如自动完成)

我正在考虑制作自定义文本视图,但如果我可以避免它,那么在我的情况下这将是理想的。谢谢!

1 个答案:

答案 0 :(得分:5)

- (NSRect)rectForActiveRange {
    NSLayoutManager *l = [self layoutManager];
    NSRange range = [l glyphRangeForCharacterRange:self.activeRange actualCharacterRange:nil];
    NSRect rect = [l boundingRectForGlyphRange:range inTextContainer:[self textContainer]];
    rect = NSOffsetRect(rect, self.textContainerOrigin.x, self.textContainerOrigin.y);
    return rect;
}

activeRange == selectedRange WHICH是选择范围或者如果从您键入的右侧选择一个长度为0的范围

另见:http://www.cocoabuilder.com/archive/cocoa/158706-how-to-find-the-insertion-point-of-nstextview.html