?oreGraphics_text_drawing_performance

  • In order to have my text visible at any background I draw it twice: once in stroke mode and then once in fill mode.
    Code sample below:

    CGContextSetTextDrawingMode(ctx, kCGTextStroke);        // Stroke mode
    [string drawAtPoint:point withFont:font];
    CGContextSetTextDrawingMode(ctx, kCGTextFill);          // Fill mode
    [string drawAtPoint:point withFont:font];

    The text of the string is changed frequently and the view to which the string is drawn is being redrawn about 10 times per second.
    It turns out that drawing text in stroke mode takes about 80% of drawing time, but the drawing in fill view happens very fast.

    I wonder if is there any more efficient way to draw the text in stroke mode, since this one seem to be quite slow.

    With kindest regards, Nazar Bartosik.
  • On 13/07/2012, at 3:15 AM, Nazar Bartosik wrote:

    > I wonder if is there any more efficient way to draw the text in stroke mode, since this one seem to be quite slow.

    You can combine fill and stroke modes and draw it in one pass by using kCGTextFillStroke

    --Graham
  • On Jul 12, 2012, at 10:15 AM, Nazar Bartosik <nebelll...> wrote:

    > In order to have my text visible at any background I draw it twice: once in stroke mode and then once in fill mode.
    > Code sample below:
    >
    > CGContextSetTextDrawingMode(ctx, kCGTextStroke);        // Stroke mode
    > [string drawAtPoint:point withFont:font];
    > CGContextSetTextDrawingMode(ctx, kCGTextFill);          // Fill mode
    > [string drawAtPoint:point withFont:font];
    >
    > The text of the string is changed frequently and the view to which the string is drawn is being redrawn about 10 times per second.
    > It turns out that drawing text in stroke mode takes about 80% of drawing time, but the drawing in fill view happens very fast.
    >
    > I wonder if is there any more efficient way to draw the text in stroke mode, since this one seem to be quite slow.

    The likely reason why stroke takes 80% of the drawing time here is that its being done first. I would suspect if you flipped the order then fill would take 80% of the drawing time. The reality is that its just the first attempt at drawing the string that is taking the majority of the time, not the manner in which it is drawn.

    But as was stated by Graham, you can set the text drawing mode to both fill & stroke at the same time.
    --
    David Duncan
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today