我目前使用以下代码来获取字形的逻辑NSView位置,这很好地工作了,但似乎有点慢,并且位置不精确地在字符字距和基线上。很可能我只需要逐个迭代字形并自己构建矩形。
这个代码(函数)现在可以运行,只是想看看是否可以更有效或正确地完成。
// 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];