Search code examples
iphonedrawingquartz-graphicsdotted-line

Draw dotted lines using Quartz on iPhone


I am developing an application in which I need to draw dotted lines between a couple of points. I tried

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound)
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 0, lengths, LENGTH_OF_ARRAY)

But I see dashed lines instead of dotted lines. How can I get dotted lines instead?


Solution

  • CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat lengths[2];
    lengths[0] = 0;
    lengths[1] = dotRadius * 2;
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, dotRadius);
    CGContextSetLineDash(context, 0.0f, lengths, 2);
    
    // CGContextAddEllipseInRect(context, self.bounds);
    

    This code should work correctly.