Draw dotted lines using Quartz on iPhone

kayvee picture kayvee · Jun 28, 2010 · Viewed 8.1k times · Source

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?

Answer

Dennis Pashkov picture Dennis Pashkov · Aug 9, 2012
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.