NSPoint / NSRect从NSTextView中的字符 [英] NSPoint/NSRect from character in NSTextView

查看:206
本文介绍了NSPoint / NSRect从NSTextView中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图得到一个NSPoint或NSRect对应一个特定字符的位置在NSTextView。这是我到目前为止(这不是很好,结果看起来是不可预测的。

So I am trying to get either an NSPoint or NSRect corresponding with the location of a specific character in an NSTextView. This is what I have so far (that isn't working very well, the results seem kind of unpredictable.

NSRange theTextRange = [[theTextView layoutManager] glyphRangeForCharacterRange:[theTextStorage editedRange] actualCharacterRange:NULL];
NSRect theTextRect = [[theTextView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[theTextView textContainer]];


推荐答案

boundingRectForGlyphRange:inTextContainer:如果你想得到相对于文本视图的矩形,你需要调整:

The rect returned by boundingRectForGlyphRange:inTextContainer: is in container coordinates. You need to adjust for this if you want to get the rect relative to the text view:

NSRange theTextRange = [[textView layoutManager] glyphRangeForCharacterRange:textRange actualCharacterRange:NULL];
NSRect layoutRect = [[textView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[textView textContainer]];
NSPoint containerOrigin = [textView textContainerOrigin];
layoutRect.origin.x += containerOrigin.x;
layoutRect.origin.y += containerOrigin.y;

这篇关于NSPoint / NSRect从NSTextView中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆