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

后端 未结 9 1126
盖世英雄少女心
盖世英雄少女心 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:29

    Here is a function I wrote that rounds the input rect using a corner radius.

    CGMutablePathRef createRoundedCornerPath(CGRect rect, CGFloat cornerRadius) {
    
        // create a mutable path
        CGMutablePathRef path = CGPathCreateMutable();
    
        // get the 4 corners of the rect
        CGPoint topLeft = CGPointMake(rect.origin.x, rect.origin.y);
        CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
        CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
        CGPoint bottomLeft = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
    
        // move to top left
        CGPathMoveToPoint(path, NULL, topLeft.x + cornerRadius, topLeft.y);
    
        // add top line
        CGPathAddLineToPoint(path, NULL, topRight.x - cornerRadius, topRight.y);
    
        // add top right curve
        CGPathAddQuadCurveToPoint(path, NULL, topRight.x, topRight.y, topRight.x, topRight.y + cornerRadius);
    
        // add right line
        CGPathAddLineToPoint(path, NULL, bottomRight.x, bottomRight.y - cornerRadius);
    
        // add bottom right curve
        CGPathAddQuadCurveToPoint(path, NULL, bottomRight.x, bottomRight.y, bottomRight.x - cornerRadius, bottomRight.y);
    
        // add bottom line
        CGPathAddLineToPoint(path, NULL, bottomLeft.x + cornerRadius, bottomLeft.y);
    
        // add bottom left curve
        CGPathAddQuadCurveToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomLeft.x, bottomLeft.y - cornerRadius);
    
        // add left line
        CGPathAddLineToPoint(path, NULL, topLeft.x, topLeft.y + cornerRadius);
    
        // add top left curve
        CGPathAddQuadCurveToPoint(path, NULL, topLeft.x, topLeft.y, topLeft.x + cornerRadius, topLeft.y);
    
        // return the path
        return path;
    }
    

    How to use the function, assuming you subclass UIView and override drawRect:

    - (void)drawRect:(CGRect)rect {
    
        // constants
        const CGFloat outlineStrokeWidth = 20.0f;
        const CGFloat outlineCornerRadius = 15.0f;
    
        const CGColorRef whiteColor = [[UIColor whiteColor] CGColor];
        const CGColorRef redColor = [[UIColor redColor] CGColor];
    
        // get the context
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        // set the background color to white
        CGContextSetFillColorWithColor(context, whiteColor);
        CGContextFillRect(context, rect);
    
        // inset the rect because half of the stroke applied to this path will be on the outside
        CGRect insetRect = CGRectInset(rect, outlineStrokeWidth/2.0f, outlineStrokeWidth/2.0f);
    
        // get our rounded rect as a path
        CGMutablePathRef path = createRoundedCornerPath(insetRect, outlineCornerRadius);
    
        // add the path to the context
        CGContextAddPath(context, path);
    
        // set the stroke params
        CGContextSetStrokeColorWithColor(context, redColor);
        CGContextSetLineWidth(context, outlineStrokeWidth);
    
        // draw the path
        CGContextDrawPath(context, kCGPathStroke);
    
        // release the path
        CGPathRelease(path);
    }
    

    Example output:

    enter image description here

提交回复
热议问题