ios 如何快速绘制一个简单的圆角矩形(圆角)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30368739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:02:23  来源:igfitidea点击:

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

iosswiftdrawing

提问by mcfly soft

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 canvas = UIGraphicsGetCurrentContext()
rec = CGRectMake(0, 0, 40, 40);

//var maskPath = UIBezierPath(roundedRect: rec, byRoundingCorners: .BottomLeft | .BottomRight, cornerRadii: CGSize(width: 3, height: 3))

CGContextAddRect(canvas, rec);
CGContextFillPath(canvas);

回答by Mukesh

//Put this code in ur drawRect

//将此代码放在你的drawRect中

Objective - C

目标-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

斯威夫特 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()

}

回答by Icaro

How to create a rounded corner rectangle using BezierPath

如何使用 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))

回答by Federico Zanetello

@mukuanswer in Swift 4.x+:

@mukuSwift 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()
}

回答by Trevor

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

SWIFT 3.x + 4.0我修改了 Swift 3 的示例并添加了几行来将矩形集中在 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/

带屏幕截图的完整代码可在:http: //lab.dejaworks.com/uiview-with-rounded-background-designable-in-storyboard/

Designable UIView with Rounded Rectangle Background

具有圆角矩形背景的可设计 UIView

回答by Aaron Halvorsen

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()

}

回答by dimpiax

Swift 4

斯威夫特 4

iOS 11

iOS 11

override func draw(_ rect: CGRect) {
    super.draw(rect)

    let context = UIGraphicsGetCurrentContext()!
    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()
}