1 /*
2 ** lineFrame: 虚线的 frame
3 ** length: 虚线中短线的宽度
4 ** spacing: 虚线中短线之间的间距
5 ** color: 虚线中短线的颜色
6 */
7 + (UIView *)createDashedLineWithFrame:(CGRect)lineFrame
8 lineLength:(int)length
9 lineSpacing:(int)spacing
10 lineColor:(UIColor *)color{
11 UIView *dashedLine = [[UIView alloc] initWithFrame:lineFrame];
12 dashedLine.backgroundColor = [UIColor clearColor];
13 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
14 [shapeLayer setBounds:dashedLine.bounds];
15 [shapeLayer setPosition:CGPointMake(CGRectGetWidth(dashedLine.frame) / 2, CGRectGetHeight(dashedLine.frame))];
16 [shapeLayer setFillColor:[UIColor clearColor].CGColor];
17 [shapeLayer setStrokeColor:color.CGColor];
18 [shapeLayer setLineWidth:CGRectGetHeight(dashedLine.frame)];
19 [shapeLayer setLineJoin:kCALineJoinRound];
20 [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:length], [NSNumber numberWithInt:spacing], nil]];
21 CGMutablePathRef path = CGPathCreateMutable();
22 CGPathMoveToPoint(path, NULL, 0, 0);
23 CGPathAddLineToPoint(path, NULL, CGRectGetWidth(dashedLine.frame), 0);
24 [shapeLayer setPath:path];
25 CGPathRelease(path);
26 [dashedLine.layer addSublayer:shapeLayer];
27 return dashedLine;
28 }