美文网首页
iOS指定圆角并带有颜色边框

iOS指定圆角并带有颜色边框

作者: 圣艾修 | 来源:发表于2020-03-05 12:02 被阅读0次

    效果图


    D5A2B2AE-F430-485E-8694-25E72C8B1796.png

    调用

        CGRect frame = CGRectMake(0, 0, self.frame.size.width, 80);
        UIView *view = [[UIView alloc] initWithFrame:frame];
        view.backgroundColor = [UIColor whiteColor];
        view.tag = kOffset;
        [Utilities setupView:view corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:10
                 borderWidth:2 borderColor:[UIColor grayColor]];
        [self addSubview:view];
    

    方法详细

    /**
     指定圆角并带有颜色边框
     view:需要添加效果的视图
     corner:圆角位置
     width:边框宽度
     color:边框颜色
     */
    + (void)setupView:(UIView *)view
              corners:(UIRectCorner)corner
         cornerRadius:(CGFloat)cornerRadius
          borderWidth:(CGFloat)width
          borderColor:(UIColor *)color {
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
        // 边框
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
        borderLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
        borderLayer.lineWidth = width;
        borderLayer.strokeColor = color.CGColor;
        borderLayer.fillColor = [UIColor clearColor].CGColor;
        // 指定圆角
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
        byRoundingCorners:corner
              cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        
        maskLayer.path = bezierPath.CGPath;
        borderLayer.path = bezierPath.CGPath;
    
        [view.layer insertSublayer:borderLayer atIndex:0];
        [view.layer setMask:maskLayer];
    
    }
    

    相关文章

      网友评论

          本文标题:iOS指定圆角并带有颜色边框

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