SirRupertIII
SirRupertIII

Reputation: 12595

When using Core Text to draw an NSAttributedString, why is my text drawn repeated?

I have an NSLayoutManager subclass, overriding - (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin and i'm trying to draw the NSAttributedString with CoreText.

This is what i see when i just call super:

enter image description here

This is what i see using Core Text code:

enter image description here

It is inverted because i removed the lines that invert the coordinates as seen in the code below. I removed those lines because i can determine if it's working without those lines and i can remove that variable from being the cause of my issues. So i'm NOT concerned about the inversion in this question.

I've experimented with different widths and heights. i've used different API's for getting the rect, but resorted to providing hard coded widths and heights, with no success.

 - (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {
    NSTextContainer *textContainer = [self textContainerForGlyphAtIndex:glyphRange.location effectiveRange:nil];
    CGRect someRect = [self boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];

    CGFloat xPoint = someRect.origin.x + origin.x;
    CGFloat width = 330;
    CGFloat calculatedHeight = 310;
    CGRect adjustedRect = CGRectMake(xPoint, someRect.origin.y + origin.y, width, calculatedHeight);

    // Create a Core Text frame setter and frame
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)storage);
    CGPathRef path = CGPathCreateWithRect(adjustedRect, NULL);
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, (CFIndex)storage.length), path, NULL);

    // Flip the coordinate system
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    // CGContextTranslateCTM(context, 0, adjustedRect.size.height + (adjustedRect.origin.y * 2));
    // CGContextScaleCTM(context, 1.0, -1.0);

    // Draw the attributed string
    CTFrameDraw(frame, context);

    // Clean up Core Text objects
    CFRelease(frame);
    CGPathRelease(path);
    CFRelease(frameSetter);

Upvotes: 0

Views: 35

Answers (0)

Related Questions