I am beginner with Core Garphics and I am try to get around with my understanding of CGContextclip. I am doing a simple program with custom UIButton drawrect as below
Rect dimension passed to drawRect method is CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef colorRef = [[UIColor blueColor] CGColor];
CGContextSetStrokeColorWithColor(context, colorRef);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextSetLineWidth(context, 3.0f);
CGContextAddRect(context, rect);
CGContextStrokeRect(context, rect);
//CGContextFillRect(context, rect);
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetFillColorWithColor(context, [[UIColor redColor]CGColor]);
CGMutablePathRef rectPath = CGPathCreateMutable();
CGPathMoveToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGPathCloseSubpath(rectPath);
CGContextAddPath(context, rectPath);
CGContextClip(context);
CGContextStrokePath(context);
//CGContextFillPath(context);
CGContextRestoreGState(context);
CGColorRelease(colorRef);
}
#end
I am expecting a full rect with dimension (0,0,100,50) colored in blue and a half rect inside with red coloured. I am getting the blue colored rect but red colored rect is not visible in my context.
I have gone through some solution here and also at some blogs but to my faulty eyes the code looks simple and fine. What is wrong with my clipping.
Thank you for your time and response.
I think there's no path in the context anymore after you clipped it.Just fill another rectangle:
//CGContextFillPath(context);
CGContextFillRect(context, rect);
Related
I want create image using CGcontext. This is simple image with white or black background. also I want to add transperent part which is in circle ( check attached image). I know how to do this in rect. But i want to make it circle. Please anyone help me in this.
Use the below code to clear circle in your context
-(UIImage *) getImageWithcenterClear:(CGPoint) center{
CGRect frame = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size,
NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5 ] CGColor]);
CGContextFillRect(context, frame);
float radius = 50 * 2;
// Clear Circle
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextAddArc(context, center.x, center.y, radius - 0.54, 0, 2 * M_PI, 0);
CGContextDrawPath(context, kCGPathFill);
CGContextSetBlendMode(context, kCGBlendModeNormal);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
I am overriding drawRect method in order to draw ellipse
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor mainColorYellow].CGColor);
CGRect rectangle = CGRectMake(0.0, 2.0, rect.size.width , rect.size.height - 4.0);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
How can I change fill color on ellipse? I tried using CGContextSetFillColorWithColor(CGContextRef c, CGColorRef color) but without result.
There is two different operations, fill and stroke. Stroke es the border, fill is the inside. You are only doing the border. You can control the fill and border color independently. You have, however, to render both explicitly. For example:
[[UIColor yellowColor] setFill];
[[UIColor blackColor] setStroke];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0.0, 2.0, rect.size.width , rect.size.height - 4.0)];
path.lineWidth = 2.0;
[path fill];
[path stroke];
Notice I wrote the code in a more modern quartz. The context is implicit. Hope it helps.
Just add below:
CGContextFillEllipseInRect(context, rectangle);
I need the same filled space around circle on the map as in Reminders app in iOS7. I think need to use the method applyFillPropertiesToContext:atZoomScale or fillPath:inContext:.
I solved my problem:
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
// Fill full map rect with some color.
CGRect rect = [self rectForMapRect:mapRect];
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:0.0 alpha:0.4f].CGColor);
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
// Clip rounded hole.
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillEllipseInRect(context, [self rectForMapRect:[self.overlay boundingMapRect]]);
CGContextRestoreGState(context);
// Draw circle
[super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
}
I wrote a simple drawRect with a intention of drawing a circle, drop a shadow and fill it with blue. I have successfully achieved drawing a circle and filling it in blue but my shadow is not in effect. Instead of fill if I stroke my circle, i see the shadow is inside the circle and during fill it is drawn over by fill colour
rect to my drawRect has dimension [[CustomButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef colorRef = [[UIColor blueColor] CGColor];
CGContextSetStrokeColorWithColor(context, colorRef);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGRect buttonRect = CGRectInset(rect, 3, 3);
CGPoint centerPoint = CGPointMake(CGRectGetMidX(buttonRect ), CGRectGetMidY(buttonRect));
CGFloat radius = CGRectGetWidth(buttonRect) / 2;
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(15.0, 20.0), 1.0);
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2*M_PI, 1);
CGContextClip(context);
CGContextFillRect(context, buttonRect);
CGContextRestoreGState(context);
CGColorRelease(colorRef);
}
#end
Thank you
Try this:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef colorRef = [[UIColor blueColor] CGColor];
CGContextSetStrokeColorWithColor(context, colorRef);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGRect buttonRect = CGRectInset(rect, 3, 3);
CGPoint centerPoint = CGPointMake(CGRectGetMidX(buttonRect ), CGRectGetMidY(buttonRect));
CGFloat radius = CGRectGetWidth(buttonRect) / 2;
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(2.0, 2.0), 2.0);
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2*M_PI, 1);
//CGContextClip(context);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGColorRelease(colorRef);
}
Your shadow was rectangular and that is why it was not seen under the circle. I changed the call CGContextFillRect to CGContextFillPath, the path which you already create using CGContextAddArc.
Is this what you were going for?
EDIT
You can find a project here: https://bitbucket.org/reydan/so_circleshadow
To draw
border at the bottom, left and right .
rounded corners at the bottom
clip to the border
Fill the colour with the actual colour, which might be set in the .xib file, but in the following code I had set the colour to clear colour.
For the the above things and the code listed below I get the following output.
A rough idea of the required output can be found as below in the image.
-(id) initWithCoder:(NSCoder *)aDecoder{
self=[super initWithCoder:aDecoder];
if(self){
self.clipsToBounds=YES;
self.backgroundColor=[UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGFloat margin=2.0;
CGRect outerRect=CGRectInset(rect, margin, margin);
CGMutablePathRef path=createRoundedRectForRect(outerRect, 10.0);
CGFloat margin1=1.0;
CGRect innerRect=CGRectInset(outerRect, margin1, margin1);
CGMutablePathRef innerPath=createRoundedRectForRect(innerRect, 5.0);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextAddPath(context, path);
CGContextAddPath(context, innerPath);
CGContextClip(context);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor
);
CGContextStrokeRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor
);
CGContextFillRect(context, innerRect);
CGContextRestoreGState(context);
}
CGMutablePathRef createRoundedRectForRect (CGRect rect, CGFloat radius){
CGMutablePathRef path=CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMinY(rect), radius);
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect),CGRectGetMinY(rect));
return path;
}
Part 2, Changes
These below changes gives me the following output, where "Add" Button still overlaps and is not clipped to the border.
The output is almost the expected one, as seen in the following image.
- (void)drawRect:(CGRect)rect
{
CGSize radii=CGSizeMake(10.0, 10.0);
UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:radii];
[[UIColor blackColor] setFill];
[path fill];
CGFloat margin=4.0;
CGRect innerRect=CGRectMake(rect.origin.x+margin, 0, rect.size.width-(2*margin), rect.size.height-margin);
//Scaling InnerRadii
CGSize radii2=CGSizeMake(radii.width-margin, radii.height-margin);
UIBezierPath *innerPath=[UIBezierPath bezierPathWithRoundedRect:innerRect byRoundingCorners:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:radii2];
[[UIColor redColor] setFill];
[innerPath fill];
}
Here is how I would do it. I would create an outer rectangle with the bottom corners rounded. Then, I would duplicate this rectangle and change its dimensions so it's slightly smaller and overlay it on top of the first rectangle.
- (void) drawRect:(CGRect)rect
{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
[[UIColor blackColor] setFill];
[path fill];
CGRect innerRect = CGRectInset(rect, 4.0f, 2.0f);
innerRect.origin.y -= 2.0f;
UIBezierPath *innerPath = [UIBezierPath bezierPathWithRoundedRect:innerRect
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0f, 8.0f)];
[[UIColor redColor] setFill];
[innerPath fill];
}
This produces the following output:
Edit: scaled the cornerRadii as Paul.s suggested.