Beispiel #1
0
        public override void LayoutSubviews()
        {
            base.LayoutSubviews();

            CGRect bounds = new CGRect()
            {
                Size = this.Bounds.Size
            };

            NSDictionary buttonDictionary = this.buttonDictionary;

            // Settings.
            /*const*/ UIUserInterfaceIdiom interfaceIdiom = UI_USER_INTERFACE_IDIOM();
            /*const*/ nfloat spacing            = (interfaceIdiom == UIUserInterfaceIdiom.Pad) ? MMNumberKeyboardPadBorder : 0.0f;
            /*const*/ nfloat maximumWidth       = (interfaceIdiom == UIUserInterfaceIdiom.Pad) ? (nfloat)400.0 : bounds.Width;
            /*const*/ bool   allowsDecimalPoint = this.allowsDecimalPoint;

            /*const*/ nfloat width       = (nfloat)Math.Min(maximumWidth, bounds.Width);
            /*const*/ CGRect contentRect = new CGRect()
            {
                X    = (nfloat)Math.Round((bounds.Width - width) / (nfloat)2.0f),
                Y    = spacing,
                Size = new CGSize()
                {
                    Width  = width,
                    Height = bounds.Height - spacing * 2.0f
                }
            };

            // Layout.
            /*const*/ nfloat columnWidth = contentRect.Width / 4.0f;
            /*const*/ nfloat rowHeight   = MMNumberKeyboardRowHeight;

            CGSize numberSize = new CGSize(columnWidth, rowHeight);

            // Layout numbers.
            /*const*/ MMNumberKeyboardButton numberMin = MMNumberKeyboardButton.NumberMin;
            /*const*/ MMNumberKeyboardButton numberMax = MMNumberKeyboardButton.NumberMax;

            /*const*/ nint numbersPerLine = 3;

            for (MMNumberKeyboardButton key = numberMin; key < numberMax; key++)
            {
                UIButton button = (UIButton)buttonDictionary["" + key];
                nint     digit  = key - numberMin;

                CGRect rect = new CGRect()
                {
                    Size = numberSize
                };

                if (digit == 0f)
                {
                    rect.Y = numberSize.Height * 3f;
                    rect.X = numberSize.Width;

                    if (!allowsDecimalPoint)
                    {
                        rect.Size = new CGSize()
                        {
                            Width = numberSize.Width * 2.0f
                        };
                        button.ContentEdgeInsets = new UIEdgeInsets(0f, 0f, 0f, numberSize.Width);
                    }
                }
                else
                {
                    nint idx = (digit - 1);

                    nint line = idx / numbersPerLine;
                    nint pos  = idx % numbersPerLine;

                    rect.Y = line * numberSize.Height;
                    rect.X = pos * numberSize.Width;
                }

                button.Frame = MMButtonRectMake(rect, contentRect, interfaceIdiom);
            }

            // Layout special key.
            UIButton specialKey = (UIButton)buttonDictionary["" + MMNumberKeyboardButton.Special];

            if (specialKey != null)
            {
                CGRect rect = new CGRect()
                {
                    Size = numberSize
                };
                rect.Y = numberSize.Height * 3f;

                specialKey.Frame = MMButtonRectMake(rect, contentRect, interfaceIdiom);
            }

            // Layout decimal point.
            UIButton decimalPointKey = (UIButton)buttonDictionary["" + MMNumberKeyboardButton.DecimalPoint];

            if (decimalPointKey != null)
            {
                CGRect rect = new CGRect()
                {
                    Size = numberSize
                };
                rect.Y = numberSize.Height * 3f;
                rect.X = numberSize.Width * 2f;

                decimalPointKey.Frame = MMButtonRectMake(rect, contentRect, interfaceIdiom);

                decimalPointKey.Hidden = !allowsDecimalPoint;
            }

            // Layout utility column.
            /*const*/ MMNumberKeyboardButton [] utilityButtonKeys = new MMNumberKeyboardButton[] { MMNumberKeyboardButton.Backspace, MMNumberKeyboardButton.Done };
            /*const*/ CGSize utilitySize = new CGSize(columnWidth, rowHeight * 2.0f);

            for (nint idx = 0; idx < utilityButtonKeys.Length; idx++)
            {
                MMNumberKeyboardButton key = utilityButtonKeys[idx];

                UIButton button = (UIButton)buttonDictionary["" + key];
                CGRect   rect   = new CGRect()
                {
                    Size = utilitySize
                };

                rect.X = columnWidth * 3.0f;
                rect.Y = idx * utilitySize.Height;

                button.Frame = MMButtonRectMake(rect, contentRect, interfaceIdiom);
            }

            // Layout separators if phone.
            if (interfaceIdiom != UIUserInterfaceIdiom.Pad)
            {
                NSArray separatorViews = this.separatorViews;

                /*const*/ nuint totalColumns       = 4;
                /*const*/ nuint totalRows          = (nuint)numbersPerLine + 1;
                /*const*/ nuint numberOfSeparators = totalColumns * totalRows - 1;

                if (separatorViews.Count != numberOfSeparators)
                {
                    separatorViews.PerformSelector(new Selector("makeObjectsPerformSelector"), NSObject.FromObject(new Selector("removeFromSuperview")));

                    NSMutableArray neueSeparatorViews = new NSMutableArray(numberOfSeparators);

                    for (nuint idx = 0; idx < numberOfSeparators; idx++)
                    {
                        UIView separator = new UIView(CGRect.Empty);
                        separator.BackgroundColor = UIColor.FromWhiteAlpha(0.0f, /*alpha*/ 0.1f);

                        this.AddSubview(separator);
                        neueSeparatorViews.Add(separator);
                    }

                    this.separatorViews = neueSeparatorViews;
                }

                /*const*/ nfloat separatorDimension = 1.0f / ((this.Window.Screen.Scale != 0) ? this.Window.Screen.Scale:  1.0f);

                separatorViews.enumerateObjectsUsingBlock((object obj, nuint idx, out bool stop) => {
                    UIView separator = (UIView)obj;

                    CGRect rect = CGRect.Empty;

                    if (idx < totalRows)
                    {
                        rect.Y = idx * rowHeight;
                        if (idx % 2 != 0)
                        {
                            rect.Size = new CGSize(rect.Size)
                            {
                                Width = contentRect.Width - columnWidth
                            };
                        }
                        else
                        {
                            rect.Size = new CGSize(rect.Size)
                            {
                                Width = contentRect.Width
                            };
                        }
                        rect.Size = new CGSize(rect.Size)
                        {
                            Height = separatorDimension
                        };
                    }
                    else
                    {
                        nuint col = (idx - totalRows);

                        rect.X    = (col + 1) * columnWidth;
                        rect.Size = new CGSize(rect.Size)
                        {
                            Width = separatorDimension
                        };

                        if (col == 1 && !allowsDecimalPoint)
                        {
                            rect.Size = new CGSize(rect.Size)
                            {
                                Height = contentRect.Height - rowHeight
                            };
                        }
                        else
                        {
                            rect.Size = new CGSize(rect.Size)
                            {
                                Height = contentRect.Height
                            };
                        }
                    }

                    separator.Frame = MMButtonRectMake(rect, contentRect, interfaceIdiom);
                    stop            = false;
                });
            }
        }