0

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);
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 1
    It's hard to understand in your image what is being "repeated." The text in the "normal" (super) example seems to be different text than in the problem sample. And the problem in the problem sample seems to be a y-offset problem between two lines rather than repeating (is the repeating happing elsewhere?) The addition of a background image also kind of muddies the example. Have you tried putting this in a smaller test app that just draws text the way you want, and see if there's a problem? I get removing the geometry flip to test, but it also makes it hard to understand what we're looking at. – Rob Napier Aug 16 '23 at 21:41
  • How many times is `drawGlyphsForGlyphRange` called and what are the values of `glyphsToShow` and `glyphRange`? – Willeke Aug 17 '23 at 09:49

0 Answers0