Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw dotted (not dashed!) line, with IBDesignable in 2017

It's easy to draw a dashed line with UIKit. So:

CGFloat dashes[] = {4, 2}; [path setLineDash:dashes count:2 phase:0]; [path stroke]; 

enter image description here

Is there any way way to draw a genuine dotted line?

enter image description here

like image 634
Fattie Avatar asked Sep 24 '14 13:09

Fattie


People also ask

How do you draw a dotted line on a path?

Select the line drop-down next to the stroke's weight. Select one of the two presets; dotted or dashed and you're done. If you want to create a custom stroke pattern, click on 'More Options' in the pop-up that opens from the preset drop-down.

How do you make a dashed line in OpenGL?

Dotted or dashed line in OpenGL is called stippled. glPushAttrib(GL_ENABLE_BIT); # glPushAttrib is done to return everything to normal after drawing glLineStipple(1, 0xAAAA); # [1] glEnable(GL_LINE_STIPPLE); glBegin(GL_LINES); glVertex3f(-.


Video Answer


1 Answers

Set the line cap style to round and set the “on” length to a tiny number.

Swift playground example:

import UIKit import PlaygroundSupport  let path = UIBezierPath() path.move(to: CGPoint(x:10,y:10)) path.addLine(to: CGPoint(x:290,y:10)) path.lineWidth = 8  let dashes: [CGFloat] = [0.001, path.lineWidth * 2] path.setLineDash(dashes, count: dashes.count, phase: 0) path.lineCapStyle = CGLineCap.round  UIGraphicsBeginImageContextWithOptions(CGSize(width:300, height:20), false, 2)  UIColor.white.setFill() UIGraphicsGetCurrentContext()!.fill(.infinite)  UIColor.black.setStroke() path.stroke()  let image = UIGraphicsGetImageFromCurrentImageContext() let view = UIImageView(image: image) PlaygroundPage.current.liveView = view  UIGraphicsEndImageContext() 

Result:

dots


For objective-C, using the same example class as in the question, simply add

CGContextSetLineCap(cx, kCGLineCapRound); 

before the call to CGContextStrokePath, and change the ra array values to match my Swift code.

like image 181
rob mayoff Avatar answered Oct 02 '22 13:10

rob mayoff