MonoTouch.Dialog: how to set the character limit for an EntryElement

I cannot find how to limit the number of characters on an EntryElement

+4
source share
4 answers

I also prefer inheritance and events :-) Try the following:

 class MyEntryElement : EntryElement { public MyEntryElement (string c, string p, string v) : base (c, p, v) { MaxLength = -1; } public int MaxLength { get; set; } static NSString cellKey = new NSString ("MyEntryElement"); protected override NSString CellKey { get { return cellKey; } } protected override UITextField CreateTextField (RectangleF frame) { UITextField tf = base.CreateTextField (frame); tf.ShouldChangeCharacters += delegate (UITextField textField, NSRange range, string replacementString) { if (MaxLength == -1) return true; return textField.Text.Length + replacementString.Length - range.Length <= MaxLength; }; return tf; } } 

but also read the Miguel warning (edit my post) here: MonoTouch.Dialog: adjust record alignment for EntryElement

+9
source

MonoTouch.Dialog does not have this function baked by default. It is best to copy and paste the code for this element and rename it as LibEventryElement. Then execute your own version of UITextField (something like Limitextext), which overrides the ShouldChangeCharacters character method. And then in the "LimitedEntryElement" element:

 UITextField entry; 

to something like:

 LimitedTextField entry; 
+1
source

I'm doing it:

 myTextView.ShouldChangeText += CheckTextViewLength; 

And this method:

 private bool CheckTextViewLength (UITextView textView, NSRange range, string text) { return textView.Text.Length + text.Length - range.Length <= MAX_LENGTH; } 
0
source

I prefer, as shown below, because I just need to specify the number of characters for each case. In this example, I settled on 12 numbers.

 this.edPhone.ShouldChangeCharacters = (UITextField t, NSRange range, string replacementText) => { int newLength = t.Text.Length + replacementText.Length - range.Length; return (newLength <= 12); }; 
0
source

Source: https://habr.com/ru/post/1383690/


All Articles