CTRunGetImageBounds returning inaccurate results

前端 未结 2 546
遇见更好的自我
遇见更好的自我 2021-02-04 20:59

I am using Core Text to draw some text. I would like to get the various run bounds, but when I call CTRunGetImageBounds, the rect that is returned is the correct si

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 21:33

    Before you can use CTRunGetImageBounds() or CTLineDraw() you need to set the starting text position with a call to CGContextSetTextPosition() before drawing or computing each line. The reason is a CTLine has no idea where to start drawing (or computing) on its own, it uses the last text position, so you need to give it an XY starting point. To get the origins for a frame's array of lines, call CTFrameGetLineOrigins() with an array of points. Then, before drawing or computing each line - set the origin of that line via CGContextSetTextPosition().

    NSArray *lines = (NSArray *)CTFrameGetLines(frame);
    
    CGPoint origins[[lines count]];                              // the origins of each line at the baseline
    CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), origins); 
    
    int i, lineCount;
    
    lineCount = [lines count];
    
    for (i = 0; i < lineCount; i++) {
     CTLineRef line = (CTLineRef)[lines objectAtIndex:i];
     CGContextSetTextPosition(context, origins[i].x, origins[i].y);
     CTLineDraw(line, context);
     }
    

提交回复
热议问题