话不多说,直接上代码:
/**
虚线边框
@param lineView 需要虚线边框的View
@param lineWidth 边框宽度
@param lineLength 边框长度
@param lineSpacing 边框间距
@param lineColor 边框颜色
@param fillColor 填充颜色
@param cornerRadius 圆角
*/
- (void)drawDashLine:(UIView *)lineView lineWidth:(int)lineWidth lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor fillColor:(UIColor *)fillColor cornerRadius:(int)cornerRadius {
CAShapeLayer *border = [CAShapeLayer layer];
//边框颜色
border.strokeColor = lineColor.CGColor;
//填充的颜色
border.fillColor = fillColor.CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:lineView.bounds cornerRadius:cornerRadius];
//设置路径
border.path = path.CGPath;
border.frame = lineView.bounds;
//边框的宽度
border.lineWidth = lineWidth;
//设置线条的样式
// border.lineCap = @"round";
//虚线的虚线长度与间隔
border.lineDashPattern = @[@(lineLength), @(lineSpacing)];
[lineView.layer addSublayer:border];
}
效果图: