You can get the rect of a character in a NSTextView
like this:
//Get the range of characters, which may be different than the range of glyphs
NSRange range = [[textView layoutManager]glyphRangeForCharacterRange:NSMakeRange(charIndex, 0) actualCharacterRange:NULL]
//Get the rect of the character
NSRect rect = [[textView layoutManager]boundingRectForGlyphRange:range inTextContainer:[textView textContainer]];
Then, get the NSGlyph
that you want:
NSGlyph glyph = [[textView layoutManager]glyphAtIndex:range.location];
And draw it in an NSBezierPath
:
NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithGlyph:glyph inFont:myFavoriteFont];
Next, query the path for its bounds:
NSRect actualRect = [path bounds];
You can then compare those two rectangles.
Have a look at the NSLayoutManager Class Reference, the Text System Overview, and the Text Layout Programming Guide.