NSLayoutManager vertical offset mystery
NSLayoutManager vertical offset mystery
- Subject: NSLayoutManager vertical offset mystery
- From: Andy O'Meara <email@hidden>
- Date: Sat, 6 Feb 2010 16:20:51 -0600
Hi gents, I have a small cocoa text layout issue that I could use some help from the veterans here, and I've been unable to make progress using google, ADC, or the list archives...
I'm rendering small words of text to a NSGraphicsContext using a vanilla NSLayoutManager/NSTextStorage/NSTextContainer outlined here:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/TextArchitecture/Tasks/SimpleTasks.html#//apple_ref/doc/uid/20001797
The purpose of the module is to image various words of text into images used elsewhere. If it matters, the NSGraphicsContext I'm using is totally vanilla and is made via graphicsContextWithBitmapImageRep (and has no affine or other transformations applied to it at any time).
So when I layout the text with NSLayoutManager's drawGlyphsForGlyphRange:atPoint using the point 0,0, the text is drawn but it's offset vertically such that I need to subtract (what seems like) half the container rect height from the y cord in order for the text to be vertically centered in the glyph rect returned by usedRectForTextContainer. There's no obvious combination of offsets that I have access to that would yield a vertical offset that places the text where it's supposed to go (namely, with the topleft of the container at context origin). For example, for the 11 point font that I'm using, I need to offset it by -5 vertically while locationForGlyphAtIndex:0 returns a y value of 11 (while baselineOffsetInLayoutManager returns 2). Strangely, if I stroke a rect path around the rect returned by usedRectForTextContainer, it expectedly draws the rect's topleft coincident with the context's origin.
As a test, I stuck in a newline in the text and the lines of text are unexpectedly in reverse order, so for the string "ABC\nDEF" draws as:
DEF
ABC
...which only seems to confuse me further.
Thanks in advance!
Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden