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; }); } }
public void _buttonInput(UIButton button) { MMNumberKeyboardButton keyboardButton = MMNumberKeyboardButton.None; //https://github.com/xamarin/monotouch-samples/blob/master/AppPrefs/Settings.cs //NSArray.FromArray<NSDictionary> ( // foreach (KeyValuePair<NSObject,NSObject> t in this.buttonDictionary.GetEnumerator<KeyValuePair<NSObject,NSObject>>()) { //foreach (var t in this.buttonDictionary.GetEnumerator()) { // this.buttonDictionary. //foreach (var item in NSArray.FromArray<NSDictionary> (this.buttonDictionary.GetEnumerator)) { for (nuint i = 0; i < this.buttonDictionary.Count; i++) { //id key, id obj, BOOL *stop NSObject key = this.buttonDictionary.Keys[i]; NSObject obj = this.buttonDictionary.Values[i]; MMNumberKeyboardButton k = (MMNumberKeyboardButton)Enum.Parse(typeof(MMNumberKeyboardButton), key.ToString()); if (button == obj) { keyboardButton = k; //stop = true; break; } } if (keyboardButton == MMNumberKeyboardButton.None) { return; } // Get first responder. IUIKeyInput keyInput = this.keyInput; MMNumberKeyboardDelegate Delegate = this.Delegate; if (!(keyInput != null)) { return; } // Handle number. /*const*/ MMNumberKeyboardButton numberMin = MMNumberKeyboardButton.NumberMin; /*const*/ MMNumberKeyboardButton numberMax = MMNumberKeyboardButton.NumberMax; if (keyboardButton >= numberMin && keyboardButton < numberMax) { NSNumber number = NSNumber.FromNInt(keyboardButton - numberMin); string @string = number.ToString(); if (Delegate.RespondsToSelector(new Selector("numberKeyboard:shouldAddText:"))) { //bool shouldAdd = Delegate.numberKeyboard(this ,/*shouldAddText*/ @string); //bool shouldAdd = Delegate.Invoke("numberKeyboard", new [] {this ,/*shouldAddText*/ @string}); bool shouldAdd = (bool)Delegate.Invoke("numberKeyboard", this, /*shouldAddText*/ @string); if (!shouldAdd) { return; } } keyInput.InsertText(@string); } // Handle backspace. else if (keyboardButton == MMNumberKeyboardButton.Backspace) { keyInput.DeleteBackward(); } // Handle done. else if (keyboardButton == MMNumberKeyboardButton.Done) { bool shouldReturn = true; if (Delegate.RespondsToSelector(new Selector("numberKeyboardShouldReturn*/ "))) { shouldReturn = (bool)Delegate.Invoke("numberKeyboardShouldReturn", this); } if (shouldReturn) { this._dismissKeyboard(button); } } // Handle special key. else if (keyboardButton == MMNumberKeyboardButton.Special) { Action handler = this.specialKeyHandler; if (handler != null) { handler(); } } // Handle . else if (keyboardButton == MMNumberKeyboardButton.DecimalPoint) { string decimalText = button.Title(UIControlState.Normal); if (Delegate.RespondsToSelector(new Selector("numberKeyboard:shouldAddText:"))) { bool shouldAdd = (bool)Delegate.Invoke("numberKeyboard", new object[] { this, /*shouldAddText*/ decimalText }); if (!shouldAdd) { return; } } keyInput.InsertText(decimalText); } }
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; }); } }
public void _commonInit() { NSMutableDictionary buttonDictionary = new NSMutableDictionary(); /*const*/ MMNumberKeyboardButton numberMin = MMNumberKeyboardButton.NumberMin; /*const*/ MMNumberKeyboardButton numberMax = MMNumberKeyboardButton.NumberMax; UIFont buttonFont = UIFont.FromName("Helvetica-Bold", 20f); if (buttonFont.RespondsToSelector(new Selector("weight:"))) { buttonFont = UIFont.SystemFontOfSize(28.0f, /*weight*/ UIFontWeight.Light); } else { buttonFont = UIFont.FromName("HelveticaNeue-Light", /*size*/ 28.0f); } UIFont doneButtonFont = UIFont.SystemFontOfSize(17.0f); //for (MMNumberKeyboardButton key = numberMin;key < numberMax; key++) { foreach (MMNumberKeyboardButton key in Enum.GetValues(typeof(MMNumberKeyboardButton))) { UIButton button = new _MMNumberKeyboardButton(MMNumberKeyboardButtonType.White); string Title = (key - numberMin).ToString(); button.SetTitle(Title, /*forState*/ UIControlState.Normal); button.TitleLabel.Font = buttonFont; int keyInt = (int)key; Console.WriteLine("keyInt:" + keyInt); var number = new NSNumber(keyInt); Console.WriteLine("number:" + number); buttonDictionary.Add(button, number); } UIImage backspaceImage = this._keyboardImageNamed("MMNumberKeyboardDeleteKey.png"); UIImage dismissImage = this._keyboardImageNamed("MMNumberKeyboardDismissKey.png"); _MMNumberKeyboardButton backspaceButton = new _MMNumberKeyboardButton(MMNumberKeyboardButtonType.Gray); backspaceButton.SetImage(/*setImage*/ backspaceImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal); backspaceButton.AddTarget(/*addTarget*/ this, /*action*/ new Selector("_backspaceRepeat: "), /*.forContinuousPressWithTimeInterval*/ 0.15f); buttonDictionary.Add(backspaceButton, /*forKey*/ NSObject.FromObject(MMNumberKeyboardButton.Backspace)); UIButton specialButton = new _MMNumberKeyboardButton(MMNumberKeyboardButtonType.Gray); buttonDictionary.Add(specialButton, /*forKey*/ NSObject.FromObject(MMNumberKeyboardButton.Special)); UIButton doneButton = new _MMNumberKeyboardButton(MMNumberKeyboardButtonType.Done); doneButton.TitleLabel.Font = doneButtonFont; doneButton.SetTitle(UIKitLocalizedString("Done"), /*forState*/ UIControlState.Normal); buttonDictionary.Add(doneButton, /*forKey*/ NSObject.FromObject(MMNumberKeyboardButton.Done)); _MMNumberKeyboardButton decimalPointButton = new _MMNumberKeyboardButton(MMNumberKeyboardButtonType.White); NSLocale locale = this.locale ?? NSLocale.CurrentLocale; string decimalSeparator = @"."; //locale.ObjectForKey(NSLocale.DecimalSeparator); decimalPointButton.SetTitle(decimalSeparator ?? ".", /*forState*/ UIControlState.Normal); buttonDictionary.Add(decimalPointButton, /*forKey*/ NSObject.FromObject(MMNumberKeyboardButton.DecimalPoint)); foreach (UIButton button in buttonDictionary.Values) { button.ExclusiveTouch = true; button.AddTarget(this, /*action*/ new Selector("_buttonInput"), /*forControlEvents*/ UIControlEvent.TouchUpInside); button.AddTarget(this, /*action*/ new Selector("_buttonPlayClick"), /*forControlEvents*/ UIControlEvent.TouchDown); this.AddSubview(button); } UIPanGestureRecognizer highlightGestureRecognizer = new UIPanGestureRecognizer(this, /*action*/ new Selector("_handleHighlightGestureRecognizer")); this.AddGestureRecognizer(highlightGestureRecognizer); this.buttonDictionary = buttonDictionary; // Add default action. this.configureSpecialKeyWithImage(dismissImage, /*target*/ this, /*action*/ new Selector("_dismissKeyboard")); // Size to fit. this.SizeToFit(); }