How to get a border on UIBezierPath

前端 未结 2 1234
花落未央
花落未央 2021-01-03 01:02

I have this

CGRect container = CGRectMake(conX, conY, 220, 50);
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:container cornerRadius:5.0]         


        
相关标签:
2条回答
  • 2021-01-03 01:23

    If you're not opposed to using layers, something like this could work for you:

    //create triangle path
    UIBezierPath* path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 30)];
    [path addLineToPoint:CGPointMake(100, 30)];
    [path addLineToPoint:CGPointMake(100, 0)];
    [path addLineToPoint:CGPointMake(0, 30)];
    
    //apply path to shapelayer 
    CAShapeLayer* greenPath = [CAShapeLayer layer];
    greenPath.path = path.CGPath;
    [greenPath setFillColor:[UIColor greenColor].CGColor];
    [greenPath setStrokeColor:[UIColor blueColor].CGColor];
    greenPath.frame=CGRectMake(0, 0,100,30);
    
    //add shape layer to view's layer
    [[self layer] addSublayer:greenPath];
    
    0 讨论(0)
  • 2021-01-03 01:35

    You can get bezierPath using this extension.

    let path = UIBezierPath(yourView.bounds, cornerRadius: 12) 
    
    let layer = CAShapeLayer()
    layer.masksToBounds = false
    layer.path = path.cgPath
    layer.strokeColor = UIColor.red.cgColor
    layer.fillColor = UIColor.clear.cgColor
    layer.lineWidth = 5
    layer.addSublayer(topLayer)
    
    
    
    extension UIBezierPath {
    
            static func borderPathInRect(_ drawRect: CGRect, cornerRadius: CGFloat) -> UIBezierPath {
                let path = UIBezierPath()
                path.move(to: CGPoint(x: drawRect.origin.x, y: cornerRadius))
    
                path.addArc(withCenter: CGPoint(x: cornerRadius, y: cornerRadius),
                            radius: cornerRadius,
                            startAngle: CGFloat.pi,
                            endAngle: 3/2*CGFloat.pi ,
                            clockwise: true)
    
                path.addLine(to: CGPoint(x: drawRect.size.width - cornerRadius, y: 0))
                path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: cornerRadius),
                            radius: cornerRadius,
                            startAngle: 3/2*CGFloat.pi,
                            endAngle:  2*CGFloat.pi,
                            clockwise: true)
    
                path.addLine(to: CGPoint(x: drawRect.size.width, y: drawRect.size.height  - cornerRadius))
                path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: drawRect.size.height - cornerRadius),
                            radius: cornerRadius,
                            startAngle: 0,
                            endAngle:  CGFloat.pi/2,
                            clockwise: true)
    
                path.addLine(to: CGPoint(x: cornerRadius, y: drawRect.size.height))
                path.addArc(withCenter: CGPoint(x: cornerRadius, y: drawRect.size.height - cornerRadius),
                            radius: cornerRadius,
                            startAngle: CGFloat.pi/2,
                            endAngle:  CGFloat.pi,
                            clockwise: true)
    
                path.addLine(to: CGPoint(x: 0, y: cornerRadius))
                path.lineJoinStyle = .round
    
                return path
            }
        }
    
    0 讨论(0)
提交回复
热议问题