private void DrawRect(CGContext g, CGRect rect, Thickness borderThickness, Brush borderBrush, Brush fill, CornerRadius cornerRadius) { var center = new CGPoint(rect.Left + cornerRadius.TopLeft + borderThickness.Left / 2, rect.Top + cornerRadius.TopLeft + borderThickness.Top / 2); var strokeColor = borderBrush != null ? borderBrush.ToUIColor(rect.Size).CGColor : UIColor.Black.CGColor; g.SetStrokeColor(strokeColor); g.SetFillColor(strokeColor); List<Arc> innerArcs = new List<Arc>(); if (cornerRadius.TopLeft != 0) { innerArcs.Add(this.DrawArc(g, cornerRadius.TopLeft, center, borderThickness.LeftF(), borderThickness.TopF(), 180f, 270f)); } else { innerArcs.Add(new Arc() { Center = center }); } g.BeginPath(); g.SetLineWidth(borderThickness.TopF()); g.MoveTo((nfloat)cornerRadius.TopLeft, borderThickness.TopF() / 2); g.AddLineToPoint(rect.Right - (nfloat)cornerRadius.TopRight - borderThickness.RightF() / 2, borderThickness.TopF() / 2); g.StrokePath(); center = new CGPoint(rect.Right - cornerRadius.TopRight - borderThickness.Right / 2, rect.Top + cornerRadius.TopRight + borderThickness.Top / 2); if (cornerRadius.TopRight != 0) { innerArcs.Add(this.DrawArc(g, cornerRadius.TopRight, center, borderThickness.TopF(), borderThickness.RightF(), -90f, 0f)); } else { innerArcs.Add(new Arc { Center = center }); } g.BeginPath(); g.SetLineWidth(borderThickness.RightF()); g.MoveTo(rect.Right - borderThickness.RightF() / 2, (nfloat)cornerRadius.TopRight); g.AddLineToPoint(rect.Right - borderThickness.RightF() / 2, rect.Height - (nfloat)cornerRadius.BottomRight); g.StrokePath(); center = new CGPoint(rect.Right - cornerRadius.BottomRight - borderThickness.Right / 2, rect.Bottom - cornerRadius.BottomRight - borderThickness.Bottom / 2); if (cornerRadius.BottomRight != 0) { innerArcs.Add(this.DrawArc(g, cornerRadius.BottomRight, center, borderThickness.RightF(), borderThickness.BottomF(), 0f, 90f)); } else { innerArcs.Add(new Arc() { Center = center }); } g.BeginPath(); g.SetLineWidth(borderThickness.BottomF()); g.MoveTo(rect.Right - (nfloat)cornerRadius.BottomRight, rect.Bottom - borderThickness.BottomF() / 2); g.AddLineToPoint(rect.Left + (nfloat)cornerRadius.BottomLeft, rect.Bottom - borderThickness.BottomF() / 2); g.StrokePath(); center = new CGPoint((rect.Left + cornerRadius.BottomLeft + borderThickness.Left / 2), (rect.Bottom - cornerRadius.BottomLeft - borderThickness.Bottom / 2)); if (cornerRadius.BottomLeft != 0) { innerArcs.Add(this.DrawArc(g, cornerRadius.BottomLeft, center, borderThickness.BottomF(), borderThickness.LeftF(), 90f, 180f)); } else { innerArcs.Add(new Arc() { Center = center }); } g.SetLineWidth((nfloat)borderThickness.Left); g.MoveTo(rect.Left + borderThickness.LeftF() / 2, rect.Bottom - (nfloat)cornerRadius.BottomLeft); g.AddLineToPoint(rect.Left + borderThickness.LeftF() / 2, rect.Top + (nfloat)cornerRadius.TopLeft); g.StrokePath(); if (fill != null) { var fillColor = fill.ToUIColor(rect.Size).CGColor; g.SetFillColor(fillColor); g.SetLineWidth(0); using (CGPath path = new CGPath()) { path.AddArc( innerArcs[0].Center.X, innerArcs[0].Center.Y, innerArcs[0].Radius, innerArcs[0].EndingAngle, innerArcs[0].StartingAngle, false); path.AddArc( innerArcs[1].Center.X, innerArcs[1].Center.Y, innerArcs[1].Radius, innerArcs[1].EndingAngle, innerArcs[1].StartingAngle, false); path.AddArc( innerArcs[2].Center.X, innerArcs[2].Center.Y, innerArcs[2].Radius, innerArcs[2].EndingAngle, innerArcs[2].StartingAngle, false); path.AddArc( innerArcs[3].Center.X, innerArcs[3].Center.Y, innerArcs[3].Radius, innerArcs[3].EndingAngle, innerArcs[3].StartingAngle, false); g.AddPath(path); g.DrawPath(CGPathDrawingMode.Fill); } } }