Dashed line border around UIView

后端 未结 23 2362
粉色の甜心
粉色の甜心 2020-12-02 04:16

How do I add dashed line border around UIView.

Something Like this

\"\"

23条回答
  •  情歌与酒
    2020-12-02 04:27

    Use CGContextSetLineDash() method.

    CGFloat dashPattern[]= {3.0, 2};
    
    context =UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // And draw with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 4.0);
    CGContextSetLineDash(context, 0.0, dashPattern, 2);
    
    CGContextAddRect(context, self.bounds);
    
    // Close the path
    CGContextClosePath(context);
    
    CGContextStrokePath(context);
    
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
    

    I think it will be helpful to you.

提交回复
热议问题