This question already has answers here:
How to draw a rounded rectangle in Core Graphics / Quartz 2D?
(9 answers)
Closed 8 years ago.
The code below in my drawRect method draws a rectangle on my view controller. I'd like to set a corner radius to this. What am I missing?
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
Try following, please:
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//CGContextFillRect(context, rectangle);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect: rectangle cornerRadius:15.0];
[bezierPath fill];
Related
I am drawing a simple filled circle using following code in draw rect.
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextFillPath(con);
it draws a circle properly. Now I want to clip the circle with another small circle in the middle so that it becomes a hollow circle and you can see whatever is behind the main circle. How can i do that?
Use the even-odd rule with CGContextEOClip()
CGSize size = rect.size;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, 0);
CGContextSaveGState(context);
CGContextAddPath(context, ([UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)].CGPath));
CGContextAddPath(context, ([UIBezierPath bezierPathWithOvalInRect:CGRectMake(size.width/4, size.height/4, size.width/2, size.height/2)].CGPath));
CGContextEOClip(context); //clip
CGContextAddPath(context, [UIBezierPath bezierPathWithRect:rect].CGPath);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillPath(context);
CGContextRestoreGState(context);
I was able to do that using the EO rule.
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, rect);
CGContextAddEllipseInRect(con, CGRectInset(rect, 40, 40));
CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
CGContextEOFillPath(con);
So, I have this code, that draws a circle with a line around it.
- (void)drawRect:(CGRect)rect
{
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 40.0);
CGContextSetRGBFillColor(contextRef, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(contextRef, 132.0/255.0, 128.0/255.0, 128.0/255.0, 1.0);
CGContextFillEllipseInRect(contextRef, rect);
CGContextStrokeEllipseInRect(contextRef, rect);
}
The problem is that half of the width of the line falls outside the rect.
So, what I would like to do, is to create an inset relative to the line width, to compensate for this.
Or maybe there is a method, that tells my line to remain inside the boundaries of the circle?
Before filling,
rect = CGRectInset(rect, 1, 1);
I am trying out a simple fill, two intersecting circles with same radius and to fill the intersection alone.Below is what I tried
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// Draw first circle
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextStrokePath(context);
// Draw second circle
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextEOClip(context);
CGContextFillPath(context);
}
My code doesn't even render second circle in context. I have gone through lot of question related to CGContextClip here in forum but most of them uses CGPath in their sample. Only CGpaths can be clipped? Any suggestions or ideas are appreciated.
Thank you
The functions CGContextStrokePath(), CGContextEOClip() and CGContextFillPath()
all clear the current path. Therefore the final CGContextFillPath() has no path to fill.
The following code should now work to draw the intersection of the circles:
// Use first circle as clipping path:
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
// Draw second circle:
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextFillPath(context);
Update: The following code fills and strokes the intersection:
CGContextSaveGState(context);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextRestoreGState(context);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathStroke);
(Original code which does not work:)
To fill and stroke a path, use CGContextDrawPath():
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathEOFillStroke);
Did you tried this
- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
and you can also try Flood Fill
How does one draw a crosshairs in an iOS app?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 0.5); // yellow line
CGContextBeginPath(context);
CGContextMoveToPoint(context, 40.0, 40.0); //start point
// Crosshairs go here?!?!?!?
// What do I fill in?
CGContextClosePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextStrokePath(context);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);
CGContextFillRect(context, rect);
}
See example image below.
This should help you get started in seeing what you are missing. You are very close. Add an ellipse and you'll be done but as others have suggested, a simple quick look at the Quart2d programming guide or any Quart2d tuts will show all of this and more.
Drawing a circle
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 40, 20);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 30, 10);
CGContextAddLineToPoint(context, 30, 30);
CGContextStrokePath(context);
}
I am drawing a circle in this way:
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
//CGContextSetRGBFillColor(contextRef, 0, 0, 1.0, 1.0);
//CGContextSetRGBStrokeColor(contextRef, 0, 0, 1.0, 1.0);
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]);
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0));
CGContextFillEllipseInRect(contextRef, circlePoint);
I wanted to know how can Make the inside of the circle empty so that I can see what there is behind it.
You mean you only want the outline? Use CGContextStrokeEllipseInRect in that case.
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]);
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0));
CGContextStrokeEllipseInRect(contextRef, circlePoint);
Swift 3
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setLineWidth(2.0)
currentContext?.setFillColor(color.CGColor)
let circleRect = CGRect(x: coordsFinal.x, y: coordsFinal.y, width: 50.0, height: 50.0)
currentContext?.strokeEllipse(in: circleRect)