给UIView添加虚线边框

怎么样给UIView添加一个虚线边框?

比如像这样:

CSDN移动问答

简单:

[yourView.layer setBorderWidth:5.0];
[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];///just add image name and create image with dashed or doted drawing and add here

这里只需要添加QuartzCore/QuartzCore.h框架,像下面一样导入.m文件:

#import <QuartzCore/QuartzCore.h>

CGContextSetLineDash()方法:

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);

    CGContextStrokePath(context);


    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);