Example #1
0
        void UpdatePosition(bool textOnly = false)
        {
            nfloat hudWidth                   = 100f;
            nfloat hudHeight                  = 100f;
            nfloat stringWidth                = 0f;
            nfloat stringHeight               = 0f;
            nfloat stringHeightBuffer         = 20f;
            nfloat stringAndImageHeightBuffer = 80f;

            CGRect labelRect = new CGRect();

            string @string = StringLabel.Text;

            // False if it's text-only
            bool imageUsed = (ImageView.Image != null) || (ImageView.Hidden);

            if (textOnly)
            {
                imageUsed = false;
            }

            if (imageUsed)
            {
                hudHeight = stringAndImageHeightBuffer + stringHeight;
            }
            else
            {
                hudHeight = (textOnly ? stringHeightBuffer : stringHeightBuffer + 40);
            }

            if (!string.IsNullOrEmpty(@string))
            {
                int lineCount = Math.Min(10, @string.Split('\n').Length + 1);

                if (IsIOS7OrNewer)
                {
                    var stringSize = new NSString(@string).GetBoundingRect(new CGSize(200, 30 * lineCount), NSStringDrawingOptions.UsesLineFragmentOrigin,
                                                                           new UIStringAttributes {
                        Font = StringLabel.Font
                    },
                                                                           null);
                    stringWidth  = stringSize.Width;
                    stringHeight = stringSize.Height;
                }
                else
                {
                    var stringSize = new NSString(@string).StringSize(StringLabel.Font, new CGSize(200, 30 * lineCount));
                    stringWidth  = stringSize.Width;
                    stringHeight = stringSize.Height;
                }



                hudHeight += stringHeight;

                if (stringWidth > hudWidth)
                {
                    hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2;
                }

                float labelRectY = imageUsed ? 66 : 9;

                if (hudHeight > 100)
                {
                    labelRect = new CGRect(12, labelRectY, hudWidth, stringHeight);
                    hudWidth += 24;
                }
                else
                {
                    hudWidth += 24;
                    labelRect = new CGRect(0, labelRectY, hudWidth, stringHeight);
                }
            }

            // Adjust for Cancel Button
            var    cancelRect     = new CGRect();
            string @cancelCaption = _cancelHud == null ? null : CancelHudButton.Title(UIControlState.Normal);

            if (!string.IsNullOrEmpty(@cancelCaption))
            {
                const int gap = 20;

                if (IsIOS7OrNewer)
                {
                    var stringSize = new NSString(@cancelCaption).GetBoundingRect(new CGSize(200, 300), NSStringDrawingOptions.UsesLineFragmentOrigin,
                                                                                  new UIStringAttributes {
                        Font = StringLabel.Font
                    },
                                                                                  null);
                    stringWidth  = stringSize.Width;
                    stringHeight = stringSize.Height;
                }
                else
                {
                    var stringSize = new NSString(@cancelCaption).StringSize(StringLabel.Font, new CGSize(200, 300));
                    stringWidth  = stringSize.Width;
                    stringHeight = stringSize.Height;
                }

                if (stringWidth > hudWidth)
                {
                    hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2;
                }

                // Adjust for label
                nfloat cancelRectY = 0f;
                if (labelRect.Height > 0)
                {
                    cancelRectY = labelRect.Y + labelRect.Height + (nfloat)gap;
                }
                else
                {
                    if (string.IsNullOrEmpty(@string))
                    {
                        cancelRectY = 76;
                    }
                    else
                    {
                        cancelRectY = (imageUsed ? 66 : 9);
                    }
                }

                if (hudHeight > 100)
                {
                    cancelRect = new CGRect(12, cancelRectY, hudWidth, stringHeight);
                    labelRect  = new CGRect(12, labelRect.Y, hudWidth, labelRect.Height);
                    hudWidth  += 24;
                }
                else
                {
                    hudWidth  += 24;
                    cancelRect = new CGRect(0, cancelRectY, hudWidth, stringHeight);
                    labelRect  = new CGRect(0, labelRect.Y, hudWidth, labelRect.Height);
                }
                CancelHudButton.Frame = cancelRect;
                hudHeight            += (cancelRect.Height + (string.IsNullOrEmpty(@string) ? 10 : gap));
            }

            HudView.Bounds = new CGRect(0, 0, hudWidth, hudHeight);
            if (!string.IsNullOrEmpty(@string))
            {
                ImageView.Center = new CGPoint(HudView.Bounds.Width / 2, 36);
            }
            else
            {
                ImageView.Center = new CGPoint(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2);
            }


            StringLabel.Hidden = false;
            StringLabel.Frame  = labelRect;

            if (!textOnly)
            {
                if (!string.IsNullOrEmpty(@string) || !string.IsNullOrEmpty(@cancelCaption))
                {
                    SpinnerView.Center = new CGPoint((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, 40.5f);
                    if (_progress != -1)
                    {
                        BackgroundRingLayer.Position = RingLayer.Position = new CGPoint(HudView.Bounds.Width / 2, 36f);
                    }
                }
                else
                {
                    SpinnerView.Center = new CGPoint((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, (float)Math.Ceiling(HudView.Bounds.Height / 2.0f) + 0.5f);
                    if (_progress != -1)
                    {
                        BackgroundRingLayer.Position = RingLayer.Position = new CGPoint(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2.0f + 0.5f);
                    }
                }
            }
        }
Example #2
0
        void UpdatePosition(bool textOnly = false)
        {
            float      hudWidth     = 100f;
            float      hudHeight    = 100f;
            float      stringWidth  = 0f;
            float      stringHeight = 0f;
            RectangleF labelRect    = new RectangleF();

            string @string = StringLabel.Text;

            // False if it's text-only
            bool imageUsed = (ImageView.Image != null) || (ImageView.Hidden);

            if (textOnly)
            {
                imageUsed = false;
            }

            if (imageUsed)
            {
                hudHeight = 80;
            }
            else
            {
                hudHeight = (textOnly ? 20 : 60);
            }

            if (!string.IsNullOrEmpty(@string))
            {
                SizeF stringSize = new NSString(@string).StringSize(StringLabel.Font, new SizeF(200, 300));
                stringWidth  = stringSize.Width;
                stringHeight = stringSize.Height;

                hudHeight += stringHeight;

                if (stringWidth > hudWidth)
                {
                    hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2;
                }

                float labelRectY = imageUsed ? 66 : 9;

                if (hudHeight > 100)
                {
                    labelRect = new RectangleF(12, labelRectY, hudWidth, stringHeight);
                    hudWidth += 24;
                }
                else
                {
                    hudWidth += 24;
                    labelRect = new RectangleF(0, labelRectY, hudWidth, stringHeight);
                }
            }

            // Adjust for Cancel Button
            var    cancelRect     = new RectangleF();
            string @cancelCaption = CancelHudButton.Title(UIControlState.Normal);

            if (!string.IsNullOrEmpty(@cancelCaption))
            {
                const int gap        = 20;
                SizeF     stringSize = new NSString(@cancelCaption).StringSize(StringLabel.Font, new SizeF(200, 300));
                stringWidth  = stringSize.Width;
                stringHeight = stringSize.Height;

                if (stringWidth > hudWidth)
                {
                    hudWidth = (float)Math.Ceiling(stringWidth / 2) * 2;
                }

                // Adjust for label
                float cancelRectY = 0f;
                if (labelRect.Height > 0)
                {
                    cancelRectY = labelRect.Y + labelRect.Height + gap;
                }
                else
                {
                    cancelRectY = (imageUsed ? 66 : 9);
                }

                if (hudHeight > 100)
                {
                    cancelRect = new RectangleF(12, cancelRectY, hudWidth, stringHeight);
                    labelRect  = new RectangleF(12, labelRect.Y, hudWidth, stringHeight);
                    hudWidth  += 24;
                }
                else
                {
                    hudWidth  += 24;
                    cancelRect = new RectangleF(0, cancelRectY, hudWidth, stringHeight);
                    labelRect  = new RectangleF(0, labelRect.Y, hudWidth, stringHeight);
                }
                CancelHudButton.Frame = cancelRect;
                hudHeight            += (cancelRect.Height + gap);
            }

            HudView.Bounds = new RectangleF(0, 0, hudWidth, hudHeight);
            if (!string.IsNullOrEmpty(@string))
            {
                ImageView.Center = new PointF(HudView.Bounds.Width / 2, 36);
            }
            else
            {
                ImageView.Center = new PointF(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2);
            }


            StringLabel.Hidden = false;
            StringLabel.Frame  = labelRect;

            if (!textOnly)
            {
                if (!string.IsNullOrEmpty(@string))
                {
                    SpinnerView.Center = new PointF((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, 40.5f);
                    if (_progress != -1)
                    {
                        BackgroundRingLayer.Position = RingLayer.Position = new PointF(HudView.Bounds.Width / 2, 36f);
                    }
                }
                else
                {
                    SpinnerView.Center = new PointF((float)Math.Ceiling(HudView.Bounds.Width / 2.0f) + 0.5f, (float)Math.Ceiling(HudView.Bounds.Height / 2.0f) + 0.5f);
                    if (_progress != -1)
                    {
                        BackgroundRingLayer.Position = RingLayer.Position = new PointF(HudView.Bounds.Width / 2, HudView.Bounds.Height / 2.0f + 0.5f);
                    }
                }
            }
        }