views:

161

answers:

1

I'm currently using the following code to fetch the logical NSView position of glyphs which is working fine however it seems a bit slow and the positions aren't precisely on the character kerning point and baseline. It very well may be the case that all I need to do is iterate the glyphs themselves one by one and build the rect myself.

This code (functions) now, just want to see if it can be done more efficiently or properly.

// 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];
+1  A: 

This is the way to do it. However if lastRange is a characterRange as you described, you have to put it through

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

first to get correct results. you also need to make sure to account for the origin of the textcontainer the text is layouted in and then do a

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

before drawing.

dom