It's easy to draw a dashed line with UIKit. So:
CGFloat dashes[] = {4, 2}; [path setLineDash:dashes count:2 phase:0]; [path stroke];
Is there any way way to draw a genuine dotted line?
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.
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(-.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With