【问题标题】:UITextField Max Length in C# with Xamarin.iOS带有 Xamarin.iOS 的 C# 中的 UITextField 最大长度
【发布时间】:2013-06-21 17:01:44
【问题描述】:

我想将可在 iOS 应用程序中的 UITextField 中输入的字符数限制为 25 个字符。

根据this post,可以像这样在Objective-C中完成:

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

如何在 C# 中使用 Xamarin.iOS 做同样的事情?

【问题讨论】:

    标签: c# xamarin.ios xamarin


    【解决方案1】:

    这是未经测试的,但我相信它可能有效:

    UITextField myField;
    myField.ShouldChangeCharacters = (textField, range, replacementString) => {
        var newLength = textField.Text.Length + replacementString.Length - range.Length;
        return newLength <= 25;
    };
    

    【讨论】:

    • 对于 UITextView ShouldChangeText 事件使用上面相同的代码
    • 这需要在谷歌的顶部。不得不做一些挖掘。谢谢罗尔夫。
    【解决方案2】:

    我最终用委托而不是兰巴表示法:

    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;
    });
    

    但我认为 Rolf 使用 lambda 的方式更易于阅读。

    【讨论】:

      【解决方案3】:

      在处理此问题 2 天后,我自己找到了解决方案。它适用于复制/粘贴案例。希望有用。

      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;
              }
          }
      

      【讨论】:

      • 效果很好。完美。
      【解决方案4】:

      确实已经有一段时间没有回答了,这是一个很好的解决方案,但可以使用 NotificationCenter 以不同的方式实现。这样,你甚至可以添加一个回调,让它像一个小组件一样在一个单独的类中工作。

      我在这里添加它以防万一它可以帮助其他人。

      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);
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        我的解决方案是使用 EditingChange 事件:

        _myUTextField.EditingChanged += (sender, e) =>{
           if (_myUTextField.Text.Length > maxLength){
              _myUTextField.Text = _myUTextField.Text.Substring(0, _myUTextField.Text.Length - 1);
           }
        };
        

        【讨论】:

        • 谢谢,它工作得很好,当您需要将 ShouldChangeCharacters 用于其他目的时很有用。
        • 很遗憾,此解决方案不适用于粘贴的内容
        猜你喜欢
        • 2014-10-03
        • 2011-02-01
        • 1970-01-01
        • 2010-09-30
        • 2015-09-30
        • 1970-01-01
        • 1970-01-01
        • 2018-03-12
        相关资源
        最近更新 更多