Exemplo n.º 1
0
        public override void DrawRect(CGRect dirtyRect)
        {
            CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort;

            context.SetFillColor(__BackgroundColor);
            context.FillRect(dirtyRect);

            base.DrawRect(dirtyRect);

            if (BorderLineWidth > 0)
            {
                NSBezierPath bounds = new NSBezierPath();
                bounds.AppendPathWithRect(dirtyRect);
                bounds.AddClip();

                bounds.LineWidth = BorderLineWidth;

                if (BorderColor != null)
                {
                    BorderColor.SetStroke();
                }

                bounds.Stroke();
            }
        }
        /// <summary>
        /// Overrides the <see cref="UIView.Draw"/> method to draw the shape view within the passed-in <paramref name="rect"/>.
        /// </summary>
        /// <param name="rect">The rectangle to draw.</param>
        public override void Draw(CGRect rect)
        {
            float x      = (float)rect.X;
            float y      = (float)rect.Y;
            float width  = (float)rect.Width;
            float height = (float)rect.Height;
            var   cx     = width / 2f;
            var   cy     = height / 2f;

            var context = UIGraphics.GetCurrentContext();

            var hasFill   = false;
            var hasStroke = false;

            var strokeWidth = 0f;

            if (BorderWidth > 0 && BorderColor.CGColor.Alpha > 0)
            {
                context.SetLineWidth(BorderWidth);
                BorderColor.SetStroke();

                hasStroke   = true;
                strokeWidth = BorderWidth;

                x      += strokeWidth / 2f;
                y      += strokeWidth / 2f;
                width  -= strokeWidth;
                height -= strokeWidth;
            }

            if (FillColor.CGColor.Alpha > 0)
            {
                FillColor.SetFill();
                hasFill = true;
            }

            switch (ShapeType)
            {
            case ShapeType.Rectangle:
            {
                DrawBox(context, x, y, width, height, CornerRadius, hasFill, hasStroke);
                break;
            }

            case ShapeType.Circle:
            {
                DrawCircle(context, cx, cy, Math.Min(height, width) / 2f, hasFill, hasStroke);
                break;
            }

            case ShapeType.Triangle:
            {
                DrawTriangle(context, rect, hasFill, hasStroke);
                break;
            }
            }
        }
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            if (!Colors.IsDarkMode)
            {
                NSBezierPath betterBounds = new NSBezierPath();
                betterBounds.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                betterBounds.AddClip();

                betterBounds.LineWidth = BorderLineWidth;

                if (BorderColor != null)
                {
                    BorderColor.SetStroke();
                }

                betterBounds.Stroke();
            }
        }
Exemplo n.º 4
0
        public override void DrawRect(CGRect dirtyRect)
        {
            if (Gradient != null)
            {
                Gradient.DrawInRect(dirtyRect, GradientAngle);
            }

            // -----------------------------------------
            nfloat textOffset   = 0f;
            nfloat imageXOffset = 0f;
            nfloat imageWidth   = 0f;

            if (Image != null)
            {
                if (Image.Size.Height > dirtyRect.Height)
                {
                    Image.Size = new CGSize(Image.Size.Height, dirtyRect.Height);
                }
                if (Image.Size.Width > dirtyRect.Width)
                {
                    Image.Size = new CGSize(dirtyRect.Width, Image.Size.Width);
                }

                imageWidth = Image.Size.Width;

                nfloat minXOffset = (dirtyRect.Height - Image.Size.Height) / 2f;

                imageXOffset = CornerRadius;
                if (imageXOffset < minXOffset)
                {
                    imageXOffset = minXOffset;
                }

                if (AttributedTitle == null)
                {
                    imageXOffset = dirtyRect.Width / 2f - Image.Size.Width / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_BeforeCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f - AttributedTitle.Size.Width / 2f - Image.Size.Width - minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Left_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f + AttributedTitle.Size.Width / 2f + minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width - minXOffset / 2f - Image.Size.Width;
                }
                else if (IconLocation == IconLocationEnum.Right)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    textOffset   = space;
                    imageXOffset = space + AttributedTitle.Size.Width + space;
                }
                else if (IconLocation == IconLocationEnum.Left)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    imageXOffset = space;
                    textOffset   = space + Image.Size.Width + space;
                }

                CGRect imgRect = new CGRect(dirtyRect.X + imageXOffset,
                                            dirtyRect.Y + dirtyRect.Height / 2f - Image.Size.Height / 2f,
                                            Image.Size.Width,
                                            Image.Size.Height);
                Image.Draw(imgRect);
            }

            if (AttributedTitle != null)
            {
                if (IconLocation == IconLocationEnum.Right_BeforeCenteredText || IconLocation == IconLocationEnum.Left_BeforeCenteredText)
                {
                    if ((imageXOffset + imageWidth) > (dirtyRect.Width - AttributedTitle.Size.Width) / 2f)
                    {
                        textOffset = imageXOffset + imageWidth;
                    }
                }

                CGRect titleRect = new CGRect(dirtyRect.X + textOffset,
                                              dirtyRect.Y + dirtyRect.Height / 2f - AttributedTitle.Size.Height / 2f - 1f,
                                              dirtyRect.Width - textOffset,
                                              AttributedTitle.Size.Height);

                this.AttributedTitle.DrawInRect(titleRect);
            }

            if (Highlighted)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }

            if (!this.Enabled && DoNotChangeColorWhenDisabled != true)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }
            // -----------------------------------------
            //base.DrawRect (dirtyRect);

            if (!Highlighted && BorderShadow != null)
            {
                BorderShadow.Set();
            }


            NSBezierPath bounds = new NSBezierPath();

            bounds.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
            bounds.AddClip();

            bounds.LineWidth = BorderLineWidth;

            if (BorderColor != null)
            {
                BorderColor.SetStroke();
            }

            bounds.Stroke();
        }