How to draw a simple rounded rect in swift (rounded corners)

前端 未结 6 1946
北海茫月
北海茫月 2020-12-25 14:39

I managed to draw a rect :-) But I don\'t know how to draw a rounded rect.

Can someone help me out with the following code how to round the rect?

let         


        
相关标签:
6条回答
  • 2020-12-25 14:57

    SWIFT 3.x + 4.0 I've modified the example for Swift 3 and added few lines to centralise the rectangle in the UIView

    func roundRect()
    {
        // Size of rounded rectangle
        let rectWidth:CGFloat = 100
        let rectHeight:CGFloat = 80
    
        // Find center of actual frame to set rectangle in middle
        let xf:CGFloat = (self.frame.width  - rectWidth)  / 2
        let yf:CGFloat = (self.frame.height - rectHeight) / 2
    
        let ctx: CGContext = UIGraphicsGetCurrentContext()!
        ctx.saveGState()
    
        let rect = CGRect(x: xf, y: yf, width: rectWidth, height: rectHeight)
        let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: rectCornerRadius).cgPath
    
        ctx.addPath(clipPath)
        ctx.setFillColor(rectBgColor.cgColor)
    
    
    
    
        ctx.closePath()
        ctx.fillPath()
        ctx.restoreGState()
    
    }
    

    Full code with screenshots is available on: http://lab.dejaworks.com/uiview-with-rounded-background-designable-in-storyboard/

    0 讨论(0)
  • 2020-12-25 15:00

    //Put this code in ur drawRect

    Objective - C

     - (void)drawRect:(CGRect)rect
    {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    
    
     CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y, width, height) cornerRadius:6].CGPath;
    CGContextAddPath(ctx, clippath);
    
    CGContextSetFillColorWithColor(ctx, self.color.CGColor);
    
    CGContextClosePath(ctx);
    CGContextFillPath(ctx);
    
    [self.color set];
    
    
    [_path closePath]; // Implicitly does a line between p4 and p1
    [_path fill]; // If you want it filled, or...
    [_path stroke]; // ...if you want to draw the outline.
    CGContextRestoreGState(ctx);
    }
    

    Swift 3

    func drawRect(rect : CGRect)
    {
    // Size of rounded rectangle
    let rectWidth = rect.width
    let rectHeight = rect.height
    
    // Find center of actual frame to set rectangle in middle
    let xf:CGFloat = (self.frame.width  - rectWidth)  / 2
    let yf:CGFloat = (self.frame.height - rectHeight) / 2
    
    let ctx: CGContext = UIGraphicsGetCurrentContext()!
    ctx.saveGState()
    
    let rect = CGRect(x: xf, y: yf, width: rectWidth, height: rectHeight)
    let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: rectCornerRadius).cgPath
    
    ctx.addPath(clipPath)
    ctx.setFillColor(rectBgColor.cgColor)
    
    
    
    
    ctx.closePath()
    ctx.fillPath()
    ctx.restoreGState()
    
    }
    
    0 讨论(0)
  • 2020-12-25 15:10

    @muku answer in Swift 4.x+:

    override func draw(_ rect: CGRect) {
      super.draw(rect)
    
      let clipPath = UIBezierPath(roundedRect: rect, cornerRadius: 6.0).cgPath
    
      let ctx = UIGraphicsGetCurrentContext()!
      ctx.addPath(clipPath)
      ctx.setFillColor(UIColor.red.cgColor)
    
      ctx.closePath()
      ctx.fillPath()
    }
    
    0 讨论(0)
  • 2020-12-25 15:12

    How to create a rounded corner rectangle using BezierPath

     var roundRect = UIBezierPath(roundedRect: <CGRect>, byRoundingCorners: <UIRectCorner>, cornerRadii: <CGSize>)
    

    Or for an example with values:

    var roundRect = UIBezierPath(roundedRect: CGRectMake(0, 0, 100, 100), byRoundingCorners:.AllCorners, cornerRadii: CGSizeMake(16.f, 16.f))
    
    0 讨论(0)
  • 2020-12-25 15:14

    Swift 5, 4

    iOS 11

    override func draw(_ rect: CGRect) {
      super.draw(rect)
    
      guard let context = UIGraphicsGetCurrentContext() else { return }
    
      context.saveGState()
      defer { context.restoreGState() }
    
      let path = UIBezierPath(
        roundedRect: rect,
        byRoundingCorners: [.topLeft, .topRight],
        cornerRadii: CGSize(width: 4, height: 4)
      )
    
      context.addPath(path.cgPath)
      context.closePath()
    
      context.setStrokeColor(UIColor.red.cgColor)
      context.strokePath()
    }
    
    
    0 讨论(0)
  • 2020-12-25 15:16
    override func draw(_ rect: CGRect) {
    
        let bezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 40.0, height: 40.0), cornerRadius: 3.0)
        UIColor.yellow.setFill()
        bezierPath.fill()
    
    }
    
    0 讨论(0)
提交回复
热议问题