LIMIT THE NUMBER OF CHARACTERS IN TEXTFIELD IN XAMARIN.IOS

mistoquackmistoquack USMember ✭✭

hello,
how do i limit the the number of characters in textfield in xamarin.ios.

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭
    UITextField textField = new UITextField();
                textField.EditingChanged += (sender, e) => {
                    UITextField selectedField = (UITextField)sender;
                    if (selectedField.Text.Length > 2) selectedField.Text.Remove(2, 1);
                };
    

    I've just wrote this now, but not tested. Let me know if it works.

  • plutonijplutonij CHMember
    UITextField textField = new UITextField();
    textField.ShouldChangeCharacters = (UITextField field, NSRange range, string ReplacementString) =>
    {
      int maxLength = 20; //use your value here....
       if(field.Text.Length + replacementString.Length > maxLength)
        {
            return false;
        }
           return true;
    }
    
Sign In or Register to comment.