美文网首页iOS技术资料iOS开发杂货铺iOS
iOS 在控件上添加虚线边框

iOS 在控件上添加虚线边框

作者: 闹钟先生的闹钟 | 来源:发表于2017-03-15 15:09 被阅读2695次

以按钮为例子,实现虚线按钮:

    CAShapeLayer *border = [CAShapeLayer layer];
    
    //虚线的颜色
    border.strokeColor = [UIColor redColor].CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    
    //设置路径
    border.path = [UIBezierPath bezierPathWithRect:self.lineButton.bounds].CGPath;
    
    border.frame = self.lineButton.bounds;
    //虚线的宽度
    border.lineWidth = 1.f;
    
    
    //设置线条的样式
    //    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@4, @2];
    
    [self.lineButton.layer addSublayer:border];
效果1

到这里基本已经OK了,但是突然发现我要的是有圆角的按钮,那就去添加圆角

    border.cornerRadius = 5.f;
    
    border.masksToBounds = YES;

然而效果是这样子的,四个角变的很奇怪


效果2

以为要在控件上添加圆角

    self.lineButton.layer.cornerRadius = 5.f;
    self.lineButton.layer.masksToBounds = YES;

然而效果依然很奇怪。


效果3

最后找资料终于得到实现效果 需要把bezierPathWithRect 替换成 bezierPathWithRoundedRect 就可以了

最终

下面全部代码

    CAShapeLayer *border = [CAShapeLayer layer];
    
    //虚线的颜色
    border.strokeColor = [UIColor redColor].CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.lineButton.bounds cornerRadius:5];
    
    //设置路径
    border.path = path.CGPath;
    
    border.frame = self.lineButton.bounds;
    //虚线的宽度
    border.lineWidth = 1.f;
    
    
    //设置线条的样式
    //    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@4, @2];
    
    self.lineButton.layer.cornerRadius = 5.f;
    self.lineButton.layer.masksToBounds = YES;
    
    [self.lineButton.layer addSublayer:border];

相关文章

  • ios关于UI

    1.ios在控件上添加虚线边框 内容很详细,涉及到虚线边框圆角问题及解决过程。 2.ios截图 ①截某个显示图片的...

  • iOS 在控件上添加虚线边框

    http://www.jianshu.com/p/12b439443b3b 以按钮为例子,实现虚线按钮: CASh...

  • iOS 在控件上添加虚线边框

    以按钮为例子,实现虚线按钮: 到这里基本已经OK了,但是突然发现我要的是有圆角的按钮,那就去添加圆角 然而效果是这...

  • 给控件添加边框

    经常会遇到给控件添加边框的需求:边框大体有两种实线和虚线 1、虚线边框的添加: 2、实现边框的添加: a、最常采用...

  • iOS 虚线边框

    给控件设置虚线边框 调用

  • iOS view添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

  • iOS view添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

  • 添加 虚线边框|单边框

    在iOS中某些控件是具备直接使用 layer.boder 属性设置边框的宽度和颜色即可添加实现边框;并且可以实现圆...

  • iOS给view添加虚线边框

    直接上代码 给某个view设置虚线边框需要再layoutSubViews中进行,不然获取不到该view的frame...

  • iOS为UIView添加虚线边框

    有时候需要为UIView添加虚线描边,本文记录一种实现方式,主要是通过对UIView的根layer添加CAShap...

网友评论

本文标题:iOS 在控件上添加虚线边框

本文链接:https://www.haomeiwen.com/subject/oxilnttx.html