どのように私は、NSTextViewの文字のピクセル位置を見つけるのですか?

StackOverflow https://stackoverflow.com/questions/869086

  •  22-08-2019
  •  | 
  •  

質問

私は現在正常に動作しているグリフの論理的なNSViewの位置を取得するために、次のコードを使用していますが、それは少し遅いようだと位置が文字カーニングポイントとベースライン上で正確ではありません。それは非常によく私が行うために必要なすべてのグリフそれ自体が一つ一つを反復し、自分自身をRECT構築である場合があります。

このコードは、(機能が)今、ちょうどそれがより効率的にするか、適切に行うことができるかどうかを確認したい。

// lastRange is any valid, bounds checked character range in an NSTextView


NSLayoutManager *layoutManager = [self layoutManager];
NSRect paragraphRect = [layoutManager boundingRectForGlyphRange:lastRange inTextContainer:[self textContainer]];

// Draw a gradient around the range.
NSGradient * gradient = [self indicatorGradient];
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetBlendMode(myContext , kCGBlendModeMultiply);
[gradient drawInRect:paragraphRect angle:90];
役に立ちましたか?

解決

これはそれを行う方法です。 lastRangeは、あなたが述べたようcharacterRangeある場合ただし、

を通してそれを配置する必要があります
lastRange = [layoutManager glyphRangeForCharacterRange:lastRange actualRange:NULL];

最初に正しい結果を取得します。 あなたはまた、テキストが中にレイアウトされtextcontainerの起源を説明するために確認する必要があり、次に行う

NSPoint containerOrigin = [[container textView] textContainerOrigin];
paragraphRect = NSOffsetRect(paragraphRect,containerOrigin.x,containerOrigin.y);

描画の前に。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top