Add Circle CALayer Inside A View Using Core Graphics iOS (Xamarin.iOS & Objective-C)


public CALayer createImage(UIColor fillerColor, UIColor borderColor, UIView view, nfloat x, nfloat y)
            // Get the size
            CGSize canvasSize = view.Frame.Size;
            nfloat scale = UIScreen.MainScreen.Scale;

            // Resize for retina with the scale factor
            canvasSize.Width *= scale;
            canvasSize.Height *= scale;

            // Create the context

            CGContext ctx = UIGraphics.GetCurrentContext();

            // setup drawing attributes
            ctx.SetLineWidth(0.5f * scale);

            // setup the circle size
            CGRect circleRect = new CGRect(0, 0, canvasSize.Width, canvasSize.Height);
            circleRect = circleRect.Inset(0.5f,0.5f);

            // Draw the Circle

            // Create Image
            UIImage retImage = UIGraphics.GetImageFromCurrentImageContext();

            CALayer aLayer = new CALayer();
            CGRect startFrame = new CGRect(x, y, view.Frame.Width  (2*x), view.Frame.Height  (2 * y));
            aLayer.Contents = retImage.CGImage;
            aLayer.Frame = startFrame;

            return aLayer;

USE: createImage(UIColor.White, UIColor.Black, cell.ImgvEndCircle, 3f, 3f)


-(CALayer *)createImage:(UIColor *)fillerColor withbordercolor: (UIColor *)borderColor withView:(UIView*)view xPosition:(CGFloat)x yPosition:(CGFloat)y{

// Get the size
CGSize canvasSize = view.frame.size;
CGFloat scale = [UIScreen mainScreen].scale;

// Resize for retina with the scale factor
canvasSize.width *= scale;
canvasSize.height *= scale;

// Create the context
CGContextRef ctx = UIGraphicsGetCurrentContext();

// setup drawing attributes
CGContextSetLineWidth(ctx, 0.5 * scale);
CGContextSetStrokeColorWithColor(ctx, borderColor.CGColor);
CGContextSetFillColorWithColor(ctx, fillerColor.CGColor);

// setup the circle size
CGRect circleRect = CGRectMake( 0, 0, canvasSize.width, canvasSize.height );
circleRect = CGRectInset(circleRect, 0.5, 0.5);

// Draw the Circle
CGContextFillEllipseInRect(ctx, circleRect);
CGContextStrokeEllipseInRect(ctx, circleRect);

// Create Image
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();

CALayer *aLayer = [CALayer layer];
CGRect startFrame = CGRectMake(x, y, view.frame.size.width-(2*x), view.frame.size.height-(2*y));
aLayer.contents = (id)retImage.CGImage;
aLayer.frame = startFrame;
return aLayer;

Add Circle CALayer Inside A View Using Core Graphics iOS (Xamarin.iOS & Objective-C)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s