How to draw a rounded rectangle in Core Graphics / Quartz 2D?

后端 未结 9 1061
盖世英雄少女心
盖世英雄少女心 2020-11-29 18:15

I need to draw an outline for a rounded rectangle. I know I can make lines and arcs, but maybe there is also a function for rounded rects?

相关标签:
9条回答
  • 2020-11-29 18:46

    CGPathCreateWithRoundedRect() will do what you want.

    CGPathRef CGPathCreateWithRoundedRect(
       CGRect rect,
       CGFloat cornerWidth,
       CGFloat cornerHeight,
       const CGAffineTransform *transform
    );
    

    Available starting in iOS 7.0

    0 讨论(0)
  • 2020-11-29 18:49

    Swift:

        let rect: CGRect = ...
    
        let path = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
        CGContextAddPath(context, path.CGPath)
        CGContextSetStrokeColorWithColor(context, UIColor.clearColor().CGColor)
        CGContextDrawPath(context, CGPathDrawingMode.FillStroke)
    
    0 讨论(0)
  • 2020-11-29 18:49

    Swift 4.2

        let lineWidth: CGFloat = 5.0
        let path = UIBezierPath(roundedRect: rect.insetBy(dx: lineWidth/2.0, dy: lineWidth/2.0), cornerRadius: 10。0)
        path.lineWidth = lineWidth
        UIColor.green.setStroke()
        path.stroke()
    
    0 讨论(0)
提交回复
热议问题