9

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?

STW
  • 44,917
  • 17
  • 105
  • 161
kayvee
  • 93
  • 1
  • 6
  • https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html – Jay Jun 28 '10 at 18:05
  • This is an old post, but the above comment doesn't actually help. That link doesn't cover drawing dots. – Daniel Farrell Jul 20 '12 at 13:48
  • 1
    Sure it does. There's a whole section on "Painting a Path" where it describes how to draw a "line dash pattern" using CGContextSetLineDash https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/doc/c_ref/CGContextSetLineDash – pinkeerach Mar 13 '13 at 17:53

2 Answers2

12
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.

Dennis Pashkov
  • 934
  • 10
  • 24
0

Please, see the following great page about the roles of line properties! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

According to the above page, here is the code for the 'dot' line like ( . . . .)

// should
CGContextSetLineCap(context, kCGLineCapRound);

// please see the role of line properties why the first should be 0 and the second should be the doulbe of the given line width
CGFloat dash[] = {0, lineWidth*2};

// the second value (0) means the span between sets of dot patterns defined by dash array
CGContextSetLineDash(context, 0, dash, 2);
alones
  • 2,848
  • 2
  • 27
  • 30