简体   繁体   English

NSTextView 和确定字形位置

[英]NSTextView and determining glyph position

I am trying to define y axis position for certain lines and have label next to them in an NSTextView.我正在尝试为某些行定义 y 轴位置,并在 NSTextView 中在它们旁边放置标签。 I am using glyphRangeForCharacterRange.我正在使用 glyphRangeForCharacterRange。

It kind of works, but the text view can have a LOT of text (even ~4000-5000 lines) and is wrapped with insets.它有点工作,但文本视图可以有很多文本(甚至~4000-5000行)并且用插图包裹。 As I scroll to stuff that is out of view, the results are wrong.当我滚动到看不见的东西时,结果是错误的。 It seems to return values without wrapping, or so I figure.它似乎没有换行就返回值,或者我想。 If I do an update on the positions while I have scrolled to corresponding text, they are calculated correctly.如果我在滚动到相应文本时对位置进行更新,则会正确计算它们。

This is the code without additional offsets:这是没有额外偏移的代码:

NSRange characterRange = NSMakeRange(startPosition, length);
NSRange range = [[self.textView layoutManager] glyphRangeForCharacterRange:characterRange actualCharacterRange:nil];
label.frame = rect;

NSRect rect = [[self.textView layoutManager] boundingRectForGlyphRange:range inTextContainer:[self.textView textContainer]];

The same problem occurs if I try to set the label positions after the document awakes, so I calculate the labels on first keypress, which isn't too nice.如果我在文档唤醒后尝试设置标签位置,则会出现同样的问题,因此我在第一次按键时计算标签,这不太好。

What would be the best way around this?解决这个问题的最佳方法是什么?

EDIT编辑

The answer to this problem was just to ensure the layout for the NSLayoutManager before retrieving glyph coordinates.这个问题的答案只是在检索字形坐标之前确保 NSLayoutManager 的布局。

[[_textView layoutManager] ensureLayoutForTextContainer:[_textView textContainer]];

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

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