13

I would like to set a limit to the number of characters that can be entered into a UITextField in an iOS app to 25 characters.

According to this post, it could be done in Objective-C like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 25) ? NO : YES;
}

How can I do this exact same thing in C# with Xamarin.iOS?

Community
  • 1
  • 1
Jason Hartley
  • 2,459
  • 1
  • 31
  • 40

5 Answers5

34

This is untested, but I believe it may work:

UITextField myField;
myField.ShouldChangeCharacters = (textField, range, replacementString) => {
    var newLength = textField.Text.Length + replacementString.Length - range.Length;
    return newLength <= 25;
};
Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
2

I ended up doing it with a delegate rather than lamba notation:

UITextField myField;
myField.ShouldChangeCharacters = new UITextFieldChange(delegate(UITextField textField, MonoTouch.Foundation.NSRange range, string replacementString) {
    int newLength = textField.Text.Length + replacementString.Length - range.Length;
    return newLength <= 25;
});

But I think that Rolf's way using lambda is easier to read.

Jason Hartley
  • 2,459
  • 1
  • 31
  • 40
2

After 2 days working with this issue, I found out the solution for it by myself. It's working with copy/paste case. Hope it will useful.

public static bool CheckTexfieldMaxLength (UITextField textField, NSRange range, string replacementString, int maxLength)
    {

        int maxLength = 10;
        int newLength = (textField.Text.Length - (int)range.Length) + replacementString.Length;
        if (newLength <= maxLength) {
            return true;
        } else {
            if (range.Length == 0 && range.Location > 0 && replacementString.Length > 0 && textField.Text.Length >= maxLength)
                return false;

            int emptySpace = maxLength - (textField.Text.Length - (int)range.Length);

            textField.Text = textField.Text.Substring (0, (int)range.Location)
            + replacementString.Substring (0, emptySpace)
            + textField.Text.Substring ((int)range.Location + (int)range.Length, emptySpace >= maxLength ? 0 : (maxLength - (int)range.Location - emptySpace));
            return false;
        }
    }
1

It's really been a while since it has been answered and it's a good solution, but it can be achieved in a different way using the NotificationCenter. This way, you can even add a callback and make it work in a separated class like a small component.

I'm adding it here just in case it can help someone else.

NSNotificationCenter.DefaultCenter.AddObserver(
        UITextField.TextFieldTextDidChangeNotification, TextChangedEvent);

private void TextChangedEvent(NSNotification notification) {
    UITextField field = (UITextField)notification.Object;
    if (notification.Object == field) {             
        if (field.Text.Length >= 25) {
            field.Text = field.Text.Substring (0, 24);
        }
    }
}
Vinicius Vieira
  • 398
  • 8
  • 18
1

My solution is using the EditingChange event:

_myUTextField.EditingChanged += (sender, e) =>{
   if (_myUTextField.Text.Length > maxLength){
      _myUTextField.Text = _myUTextField.Text.Substring(0, _myUTextField.Text.Length - 1);
   }
};
El0din
  • 3,208
  • 3
  • 20
  • 31