public MaskValidatePage () { var rules = new List<Validation> (); rules.Add (new Validation(Validators.MAX, "10", "Max length is 10")); maxLength = new MyEntry (); maxLength.Text = ""; maxLength.FormatCharacters = "-"; maxLength.ValidationRules = rules; var rules2 = new List<Validation> (); rules2.Add (new Validation(Validators.ONLYCHARS, "[1-9]", "Only enter 1,2,3,4,5,6,7,8,9")); maxOnlyChars = new MyEntry (); maxOnlyChars.Text = ""; maxOnlyChars.FormatCharacters = "-"; maxOnlyChars.ValidationRules = rules2; // validation of a mask. beta. // maskValidation = new MyEntry (); // maskValidation.Text = "Asasd"; // maskValidation.FormatCharacters = "-"; // maskValidation.ValidCharacters = "[A-Za-z]"; // maskValidation.Mask = new System.Collections.Generic.List<MaskRules> ( // new[] { // new MaskRules { // Start = 0, // End = 5, // Mask = "", // startC = "ABC", // startCCase = false, // endC = "bC", // endCase = false // }, // new MaskRules { Start = 5, End = 9, Mask = "{0:5}-{5:}"}, // }); // add event handler. // in production code, make sure you unsubscribe to this event maxLength.OnValidationError += MaxLength_OnValidationError; maxOnlyChars.OnValidationError += MaxLength_OnValidationError; this.Content = new StackLayout { Children = { new Label { Text = "Max Length = 10", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maxLength, new Label { Text = "Max Length = 10, Only [1-9]", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maxOnlyChars, } }; }
public MyMask() { maxLength = new MyEntry (); maxLength.Text = ""; maxLength.Keyboard = Keyboard.Numeric; maxLength.Text = ""; maxLength.MaxLength = 10; entry = new MyEntry (); entry.Text = ""; entry.Keyboard = Keyboard.Numeric; entry.Text = ""; maskEntry = new MyEntry (); maskEntry.Text = ""; maskEntry.Keyboard = Keyboard.Numeric; maskEntry.Text = ""; maskEntry.FormatCharacters = "-"; maskEntry.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { new MaskRules { Start = 0, End = 3, Mask = "" }, new MaskRules { Start = 4, End = 6, Mask = "{0:3}-{3:}"}, new MaskRules { Start = 7, End = 10, Mask = "{0:3}-{3:3}-{6:}"}, new MaskRules { Start = 10, End = 20, Mask = "{0:}"} }); defaultMask = new MyEntry (); defaultMask.Text = ""; defaultMask.FormatCharacters = "-"; defaultMask.Keyboard = Keyboard.Numeric; defaultMask.Text = "1234567890"; defaultMask.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { new MaskRules { Start = 0, End = 3, Mask = "" }, new MaskRules { Start = 4, End = 6, Mask = "{0:3}-{3:}"}, new MaskRules { Start = 7, End = 10, Mask = "{0:3}-{3:3}-{6:}"}, new MaskRules { Start = 10, End = 20, Mask = "{0:}"} }); this.Content = new StackLayout { Children = { new Label { Text = "Normal Entry" }, entry, new Label { Text = "Max length = 10" }, maxLength, new Label { Text = "Mask" }, maskEntry, new Label { Text = "Apply Mask to Text" }, defaultMask } }; }
static void TextUpChanged(BindableObject bindable, object oldValue, object newValue) { MyEntry ctrl = (MyEntry)bindable; if (newValue != null && String.IsNullOrEmpty(newValue.ToString()) == false) { var txt = newValue.ToString(); ctrl.TextUpdate = ""; ctrl.LastText = ""; ctrl.SelectionStart = 0; ctrl.BeforeChars = ""; ctrl.Text = txt; ctrl.ApplyTextChangeMask(ctrl.Text, txt); } }
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e) { base.OnElementChanged (e); if (native == null) { source = e.NewElement as MyEntry; var control = (MyEntry)this.Element; native = this.Control as UITextField; if (source.FormatCharacters != null && String.IsNullOrEmpty (source.Text) == false) { ApplyDefaultRule (); } native.ShouldChangeCharacters += TextShouldChangeCharacters; } }
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e) { base.OnElementChanged (e); if (native == null) { source = e.NewElement as MyEntry; native = this.Control as EntryEditText; if (source.FormatCharacters != null && String.IsNullOrEmpty(source.Text) == false) { ApplyDefaultRule (); } native.AfterTextChanged += Native_AfterTextChanged; native.KeyPress += Native_KeyPress; native.BeforeTextChanged += Native_BeforeTextChanged; SetNativeControl(native); } }
public MyMask () { entry = new MyEntry (); entry.Text = ""; entry.Keyboard = Keyboard.Numeric; entry.Text = ""; // maxlength text maxLength = new MyEntry (); maxLength.Text = ""; maxLength.Keyboard = Keyboard.Numeric; maxLength.Text = ""; maxLength.MaxLength = 10; // zip code mask: 19201 or 12901-2010 maskZipCode = new MyEntry (); maskZipCode.Text = ""; maskZipCode.Keyboard = Keyboard.Numeric; maskZipCode.Text = ""; maskZipCode.FormatCharacters = "-"; maskZipCode.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { new MaskRules { Start = 0, End = 5, Mask = "" }, new MaskRules { Start = 5, End = 9, Mask = "{0:5}-{5:}"}, }); // mask: 0-1-3 maskEntry2 = new MyEntry (); maskEntry2.Text = ""; maskEntry2.Keyboard = Keyboard.Numeric; maskEntry2.Text = ""; maskEntry2.FormatCharacters = "-"; maskEntry2.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { new MaskRules { Start = 0, End = 1, Mask = ""}, new MaskRules { Start = 1, End = 2, Mask = "{0:1}-{1:}"}, new MaskRules { Start = 2, End = 3, Mask = "{0:1}-{1:1}-{2:}" }, }); // mask: 01/03/2015 maskEntry3= new MyEntry (); maskEntry3.Text = ""; maskEntry3.Keyboard = Keyboard.Text; maskEntry3.FormatCharacters = "/"; maskEntry3.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { // 01 [characters: 0,1] new MaskRules { Start = 0, End = 2, Mask = "{0:2}" }, // 01/03 [characters: 0,1]-[characters: 2,3] new MaskRules { Start = 2, End = 4, Mask = "{0:2}/{2:2}"}, // 01/01/2015 [characters: 0,1]-[characters: 2,3]-characters: 4,5,6,7] // max length: end=8 // {0:2} : take substring (0, 1): 01 // {2:2} : take substring (2, 2): 03 // {5:} : take substring (5) : 2015 new MaskRules { Start = 4, End = 8, Mask = "{0:2}/{2:2}/{4:}"} }); // mask: 123-345-4444 or 1233454444900 (overflow mask) defaultMask = new MyEntry (); defaultMask.Text = ""; defaultMask.FormatCharacters = "-"; defaultMask.Keyboard = Keyboard.Numeric; defaultMask.Text = "1234567890"; defaultMask.Mask = new System.Collections.Generic.List<MaskRules> ( new[] { new MaskRules { Start = 0, End = 3, Mask = "" }, new MaskRules { Start = 4, End = 6, Mask = "{0:3}-{3:}"}, new MaskRules { Start = 7, End = 10, Mask = "{0:3}-{3:3}-{6:}"}, new MaskRules { Start = 10, End = 20, Mask = "{0:}"} }); this.Content = new StackLayout { Children = { new Label { Text = "Normal Entry" }, entry, new Label { Text = "Max length = 10" , TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maxLength, new Label { Text = "Zip code Mask", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maskZipCode, new Label { Text = "0-1-2 Mask", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maskEntry2, new Label { Text = "DOB Mask", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, maskEntry3, new Label { Text = "123-345-4444 or 1233454444900 (overflow mask)", TextColor = Device.OnPlatform(Color.Blue, Color.Default, Color.Default) }, defaultMask, new Button { Text = "Mask Values (overflow)", Command = new Command(async () => { var text = defaultMask.Text; var raw = defaultMask.RawText; await DisplayAlert("Overflow Mask values", "Text:=" + text + "\r\nRaw:=" + raw, "Cancel"); }) } } }; }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged (e); if (native == null) { source = e.NewElement as MyEntry; native = this.Control as EntryEditText; // INIT defaults rules = source.Mask; FormatCharacters = source.FormatCharacters.ToCharArray (); if (FormatCharacters != null && String.IsNullOrEmpty(source.Text) == false) { ApplyDefaultRule (); } native.AfterTextChanged += (object sender, global::Android.Text.AfterTextChangedEventArgs e2) => { if (pt != null && FormatCharacters != null) { if (pt.Start != -1) { if (pt.End != -1) { native.SetSelection (pt.Start, pt.End); } else { if (pt.Start >= native.Text.Length) { pt.Start = native.Text.Length; } else { var before = source.BeforeChars; if (before == "") { pt.Start = 1; } else { var text = native.Text; for (int i = 0; i < text.Length; i++) { string c = text[i].ToString(); if (FormatCharacters.Where(ch => ch.ToString() == c.ToString()).Count() <= 0) { // no placeholder1 if (before[0].ToString() == c) { before = before.Substring(1); } if (String.IsNullOrEmpty(before)) { pt.Start = i+1; break; } } } } } native.SetSelection(pt.Start); } } pt = null; } }; native.KeyPress += (object sender, KeyEventArgs args) => { if (args.Event.Action == global::Android.Views.KeyEventActions.Down ) { var len = native.Text.Length; if (args.KeyCode == global::Android.Views.Keycode.Back || args.KeyCode == global::Android.Views.Keycode.Del) { // do test cleanup if (source.Locked == false && source.Mask != null) { source.Delete = true; args.Handled = false; } else if(source.MaxLength > 0) { args.Handled = false; } else { args.Handled = false; } } else if (source.Locked == false && source.Mask != null) { source.Delete = false; var start = native.SelectionStart; if (start < len) { var evt = args.Event; var act = evt.Action; var newChar = ((char)evt.UnicodeChar).ToString(); //var newChar = ((char)args.KeyCode.ConvertToString()).ToString(); native.Text = native.Text.Insert(start, newChar); args.Handled = true; } else { args.Handled = false; } } else if(source.MaxLength > 0) { if (len+1 > source.MaxLength) { args.Handled = true; //source.Validate("MAX", "Max length is " + source.MaxLength); } else { args.Handled = false; } } else { args.Handled = false; } } }; native.BeforeTextChanged += (object sender, global::Android.Text.TextChangedEventArgs e2) => { if (source.Locked == false && source.Mask != null) { source.SelectionStart = native.SelectionStart; source.SelectionEnd = native.SelectionEnd; source.TextLength = native.Text.Length; } else if(source.Mask == null) { source.SelectionStart = native.SelectionStart; source.SelectionEnd = native.SelectionEnd; source.TextLength = native.Text.Length; } }; SetNativeControl(native); } }
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); //var native = (UITextField)this.Control; if (native == null) { source = e.NewElement as MyEntry; var control = (MyEntry)this.Element; native = this.Control as UITextField; // INIT defaults rules = source.Mask; FormatCharacters = source.FormatCharacters.ToCharArray(); // Events //if (FormatCharacters != null && String.IsNullOrEmpty(source.Text) == false) { // ApplyDefaultRule (); //} SetFont(control); SetTextAlignment(control); SetBorder(control); SetPlaceholderTextColor(control); ResizeHeight(); native.ShouldChangeCharacters += new UITextFieldChange(delegate(UITextField textField, NSRange range, string replacementString) { native.BecomeFirstResponder(); if (source.MaxLength > 0) { var newlength = source.Text.Length + replacementString.Length - range.Length; return newlength <= source.MaxLength; } if (source.Locked) { return false; } if (source.Locked == false && source.Mask != null) { source.Delete = false; source.SelectionStart = textField.GetOffsetFromPosition(textField.BeginningOfDocument, textField.SelectedTextRange.Start); source.SelectionEnd = textField.GetOffsetFromPosition(textField.BeginningOfDocument, textField.SelectedTextRange.End); source.TextLength = native.Text.Length; } if (source.Mask != null) { source.Delete = false; if (replacementString == "") { source.Delete = true; } source.SelectionStart = textField.GetOffsetFromPosition(textField.BeginningOfDocument, textField.SelectedTextRange.Start); source.SelectionEnd = textField.GetOffsetFromPosition(textField.BeginningOfDocument, textField.SelectedTextRange.End); source.TextLength = native.Text.Length; } return true; }); //SetNativeControl(native); } }
private void SetTextAlignment(MyEntry view) { switch (view.XAlign) { case TextAlignment.Center: Control.TextAlignment = UITextAlignment.Center; break; case TextAlignment.End: Control.TextAlignment = UITextAlignment.Right; break; case TextAlignment.Start: Control.TextAlignment = UITextAlignment.Left; break; } }
void SetPlaceholderTextColor(MyEntry view) { /* UIColor *color = [UIColor lightTextColor]; YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"PlaceHolder Text" attributes:@{NSForegroundColorAttributeName: color}]; */ if (string.IsNullOrEmpty(view.Placeholder) == false && view.PlaceholderTextColor != Xamarin.Forms.Color.Default) { NSAttributedString placeholderString = new NSAttributedString(view.Placeholder, new UIStringAttributes() { ForegroundColor = view.PlaceholderTextColor.ToUIColor() }); Control.AttributedPlaceholder = placeholderString; } }
private void SetFont(MyEntry view) { UIFont uiFont; if (view.Font != Font.Default && (uiFont = view.Font.ToUIFont()) != null) Control.Font = uiFont; else if (view.Font == Font.Default) Control.Font = UIFont.SystemFontOfSize(17f); }
private void SetBorder(MyEntry view) { Control.BorderStyle = view.HasBorder ? UITextBorderStyle.RoundedRect : UITextBorderStyle.None; }