rljdxq
Last Updated: February 25, 2016
·
3.477K
· ferbass

Create a dashed border using CoreGraphics on iOS

In your custom view subclass puts thats code on drawRect method.

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, [[UIColor grayColor] CGColor]);

    CGFloat dashes[] = {1,1};

    CGContextSetLineDash(context, 0.0, dashes, 2);
    CGContextSetLineWidth(context, 1.0);

    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
    CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
    CGContextSetShouldAntialias(context, NO);
    CGContextStrokePath(context);
}