iOS给控件画虚线边框

    CGFloat viewWidth = 200;
    CGFloat viewHeight = 50;
    UIButton *view = [[UIButtonalloc]initWithFrame:CGRectMake((screenSize.width - viewWidth)/2, (screenSize.height - viewHeight) / 2, viewWidth, viewHeight)];

CAShapeLayer *borderLayer = [CAShapeLayerlayer];
borderLayer.bounds = CGRectMake(0, 0, viewWidth, viewHeight);//虚线框的大小
borderLayer.position = CGPointMake(CGRectGetMidX(view.bounds),CGRectGetMidY(view.bounds));//虚线框锚点

 borderLayer.path = [UIBezierPathbezierPathWithRect:borderLayer.bounds].CGPath;//矩形路径


 borderLayer.lineWidth = 1. / [[UIScreen mainScreen] scale];//虚线宽度

    //虚线边框
    borderLayer.lineDashPattern = @[@5, @5];
    //实线边框
    //    borderLayer.lineDashPattern = nil;
    borderLayer.fillColor = [UIColorclearColor].CGColor;
    borderLayer.strokeColor = [UIColorgrayColor].CGColor;
    [view.layer addSublayer:borderLayer];

    [self.viewaddSubview:view];

//画直线
    CGMutablePathRef path =CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x , startPoint.y);//设置划线起点
    CGPathAddLineToPoint(path, NULL, startPoint.x, lineSize.height +startPoint.y);//设置划线终点

你可能感兴趣的:(iOS学习)