3

this is my drawLayer method in a CALayer's delegate.

it's only responsible for drawing a string with length = 1.

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
    {
        CGRect boundingBox = CGContextGetClipBoundingBox(ctx);
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.letter attributes:[self attrs]];

        CGContextSaveGState(ctx);

        CGContextSetShadowWithColor(ctx, CGSizeZero, 3.0, CGColorCreateGenericRGB(1.0, 1.0, 0.922, 1.0));
        CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)string);
        CGRect rect = CTLineGetImageBounds(line, ctx);
        CGFloat xOffset = CGRectGetMidX(rect);
        CGFloat yOffset = CGRectGetMidY(rect);
        CGPoint pos = CGPointMake(CGRectGetMidX(boundingBox) - xOffset, CGRectGetMidY(boundingBox)- yOffset);
        CGContextSetTextPosition(ctx, pos.x, pos.y);

        CTLineDraw(line, ctx);

    CGContextRestoreGState(ctx);
}

here's the attributes dictionary:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:@"GillSans-Bold" size:72.0], NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
[NSNumber numberWithFloat:1.0], NSStrokeWidthAttributeName,
[NSColor blackColor], NSStrokeColorAttributeName,
style, NSParagraphStyleAttributeName, nil];

as is, the stroke does not draw, but the fill does.

if i comment out the stroke attributes in the dictionary, the fill draws.

i know this can't be right, but i can't find any reference to this problem.

is this a known issue when drawing text with a delegate ?

as the string is one character, i was following the doc example not using any framesetter machinery, but tried that anyway as a fix attempt without success.

lulu
  • 669
  • 10
  • 26

1 Answers1

3

in reading this question's answer, i realized that i needed to be using a negative number for the stroke value. i was thinking of the stroke being applied to the outside of the letter drawn by CTLineDraw, rather then inside the text shape.

i'm answering my own question, in case this should help anyone else with this misunderstanding, as i didn't see the referenced doc covering this.

Community
  • 1
  • 1
lulu
  • 669
  • 10
  • 26