How to find the pixel position of a character in an NSTextView?

I am currently using the following code to get the logical position of the NSView glyphs, which works fine, but it seems to be a bit slow and the positions are not exactly at the kerning point and baseline. Very well, maybe all I need to do is to cycle through the glyphs one at a time and build myself.

This code (function) now just wants to see if it can be done more efficiently or correctly.

// 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];
+3
source share
1 answer

This is the way to do it. However, if lastRange is a character symbol, as you described, you must execute it through

lastRange = [layoutManager glyphRangeForCharacterRange:lastRange actualRange:NULL];

, . , textcontainer ,

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

.

+3

Source: https://habr.com/ru/post/1708418/


All Articles