NSAttributedString location for drawing, glyph location and baseline

  • Hi,

    I have a subclass of NSAttachmentCell which draws itself into an NSTextView. Its y-location is wrong though when drawing the cell using

    - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *) controlView characterIndex:(NSUInteger)charIndex layoutManager: (NSLayoutManager *)layoutManager

    How do I determine the correct origin for cellFrame to ensure that the baseline of the text inside the NSTextView matches that of the text inside the cell to be drawn in the textview? In above call I use

    [self.displayWord drawInRect:cellFrame withAttributes:stringAttributes];

    to draw the string which in turn uses boundingRectWithSize to figure out the NSRect dimensions to accommodate the string?

    Thanks
  • I think I found it in, at least it looks correct:

    CGFloat gb = [[NSTypesetter sharedSystemTypesetter] baselineOffsetInLayoutManager:[(NSTextView *)controlView layoutManager] glyphIndex:charIndex];

    NSRect useFrame = cellFrame;
    useFrame.origin.y += gb;

    Thanks


    Am 15.07.2012 um 23:40 schrieb Alexander Reichstadt:

    > Hi,
    >
    > I have a subclass of NSAttachmentCell which draws itself into an NSTextView. Its y-location is wrong though when drawing the cell using
    >
    > - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *) controlView characterIndex:(NSUInteger)charIndex layoutManager: (NSLayoutManager *)layoutManager
    >
    > How do I determine the correct origin for cellFrame to ensure that the baseline of the text inside the NSTextView matches that of the text inside the cell to be drawn in the textview? In above call I use
    >
    > [self.displayWord drawInRect:cellFrame withAttributes:stringAttributes];
    >
    > to draw the string which in turn uses boundingRectWithSize to figure out the NSRect dimensions to accommodate the string?
    >
    > Thanks
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today