繁体   English   中英

获取当前的插入位置可可

[英]Get Current Caret Position Cocoa

我正在构建一个在后台运行的桌面Mac应用程序。 现在,当用户按下键盘快捷键时,将在右上角显示一个窗口,其中包含在“当前插入号”位置插入内容的几个选项。

所有这些工作都很好,但是我认为如果我可以使此选项框出现在插入符号上方,则该应用程序会更好。 如何在显示窗口时将当前插入符号的位置转换成一组变量?

在此处输入图片说明

像这样(未经测试):

NSRange selection = [textView selectedRange];
NSLayoutManager* layoutManager = [textView layoutManager];
NSTextContainer* textContainer = [textView textContainer];
NSRange glyphRange = [layoutManager glyphRageForCharacterRange:selection actualCharacterRange:nil];
NSRect boundingRect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];

boundingRect位于textContainer坐标中,因此您必须将它们转换为对您更有用的东西。 [textView textContainerOrigin]在这里很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM