4

I cannot find how to cap the number of characters on EntryElement

poupou
  • 43,413
  • 6
  • 77
  • 174
iLemming
  • 34,477
  • 60
  • 195
  • 309

4 Answers4

9

I prefer inheritance and events too :-) Try this:

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 Miguel's warning (edit to my post) here: MonoTouch.Dialog: Setting Entry Alignment for EntryElement

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174
  • Nice catch, should've noted that ShouldChangeCharacters was also exposed as a delegate property :-) – Anuj Nov 30 '11 at 00:53
  • Hopefully we'll get to the point, sooner than later, where they are all exposed in a more .NET-ish way :-) – poupou Nov 30 '11 at 01:01
  • 4
    Added the override for CellKey, otherwise it will mix this NyEntryElements with others during cell recycling. – miguel.de.icaza Nov 30 '11 at 15:53
  • How can I hookup to the ShouldChangeCharacters event on the custom entry element above? – Goober Jun 30 '13 at 10:45
1

MonoTouch.Dialog does not have this feature baked in by default. Your best bet is to copy and paste the code for that element and rename it something like LimitedEntryElement. Then implement your own version of UITextField (something like LimitedTextField) which overrides the ShouldChangeCharacters characters method. And then in "LimitedEntryElement" change:

UITextField entry;

to something like:

LimitedTextField entry;
Anuj
  • 3,134
  • 13
  • 17
  • that's a headache... Why Miguel and the guys haven't choose event based approach? Everything has to be done by overriding some methods... Darn it – iLemming Nov 29 '11 at 20:20
  • 1
    I agree, it's not exactly clean. That being said it's actually an Apple pattern that's carried over in MonoTouch. I would file a enhancement request for this as I'm almost certain we're not the only two who have this concern :-P – Anuj Nov 29 '11 at 20:25
  • 3
    Well, Apple patterns suck. No wonder why so many people use monotouch – iLemming Nov 29 '11 at 20:37
0

I do this:

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;
}
daniherculano
  • 373
  • 1
  • 9
  • 26
0

I prefer like below because I just need to specify the number of characters for each case. In this sample I settled to 12 numbers.

this.edPhone.ShouldChangeCharacters = (UITextField t, NSRange range, string replacementText) => {
    int newLength = t.Text.Length + replacementText.Length - range.Length;
    return (newLength <= 12);
};
Pang
  • 9,564
  • 146
  • 81
  • 122
Arthur Accioly
  • 801
  • 9
  • 26