View的虚线边框

2017-12-07  本文已影响0人  izsm
话不多说,直接上代码:
/**
 虚线边框

 @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];
}

效果图:


Simulator Screen Shot - iPhone 8 Plus - 2017-12-07 at 13.49.41.png
上一篇 下一篇

猜你喜欢

热点阅读