Search code examples
objective-ccore-text

Breaking a CTFrame into CTLines


I have the following code which draws an attributed string in a rect:

CGContextRef context = UIGraphicsGetCurrentContext();
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);

    // left column form
    CGMutablePathRef leftColumnPath = CGPathCreateMutable();
    CGPathAddRect(leftColumnPath, NULL, CGRectMake(rect.origin.x, -rect.origin.y,rect.size.width, self.bounds.size.height));

    // left column frame
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);   
    CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), leftColumnPath, NULL);
    CTFrameDraw(leftFrame, context);

Everything works fine here, but I need finer control over each individual line. So instead of drawing the frame, I want to draw each line separately. So I'm trying instead of using CTFrameDraw using this:

CFArrayRef linesArray =  CTFrameGetLines(leftFrame);
    for (CFIndex i = 0; i < CFArrayGetCount(linesArray); i++){
        CTLineRef line = CFArrayGetValueAtIndex(linesArray, i);
        CTLineDraw(line, context);
}

However, this doesn't draw the same result as drawing the frame. I'm a newbie to Core Text, so is there more I should be doing to draw the line? Current this loop executes several times, but only one line is drawn on the bottom of the screen (rather on top with CTFrameDraw)


Solution

  • insert CGContextSetTextPosition(context, x, y) just before CTLineDraw and change x & y as needed (don't forget y is reversed)