ShouldChangeTextInRange is not called for UITextVi

2019-07-03 06:46发布

问题:

I user custom UITextView and need to hide keyboard on return click. I need to catch 'ShouldChangeTextInRange' what has textview, I don't know why but method is not called. here is code of for my text view :

     public class PlaceholderTextView : UITextView
        { 
public PlaceholderTextView ()
        {
            Initialize ();
        }

        public PlaceholderTextView (CGRect frame)
            : base (frame)
        {
            Initialize ();
        }

        public PlaceholderTextView (IntPtr handle)
            : base (handle)
        {
            Initialize ();
        }

        void Initialize ()
        {
            Text = Placeholder;

            ShouldBeginEditing = t => {
                if (Text == Placeholder)
                    Text = string.Empty;

                return true;
            };

            ShouldEndEditing = t => {
                if (string.IsNullOrEmpty (Text))
                    Text = Placeholder;
                return true;
            };

        }
        public override bool ShouldChangeTextInRange (UITextRange inRange, string replacementText)
                {
                    if (Text.Equals ("\n")) {
                        this.EndEditing (true);
                        return false;
                    } else {
                        return true;
                    }
                }
        }

回答1:

In your UITextView subclass, add IUITextViewDelegate and implement ShouldChangeText (selector = textView:shouldChangeTextInRange:replacementText:):

public class MyTextView : UITextView, IUITextViewDelegate
{
    string Placeholder;

    public MyTextView()
    {
        Initialize();
    }

    public MyTextView(Foundation.NSCoder coder) : base(coder)
    {
        Initialize();
    }

    public MyTextView(Foundation.NSObjectFlag t) : base(t)
    {
        Initialize();
    }

    public MyTextView(IntPtr handle) : base(handle)
    {
        Initialize();
    }

    public MyTextView(CoreGraphics.CGRect frame) : base(frame)
    {
        Initialize();
    }

    public MyTextView(CoreGraphics.CGRect frame, NSTextContainer textContainer) : base(frame, textContainer)
    {
        Initialize();
    }

    void Initialize()
    {
        Delegate = this;
    }

    [Export("textViewShouldBeginEditing:")]
    public bool ShouldBeginEditing(UITextView textView)
    {
        if (Text == Placeholder)
            Text = string.Empty;

        return true;
    }

    [Export("textViewShouldEndEditing:")]
    public bool ShouldEndEditing(UITextView textView)
    {
        if (string.IsNullOrEmpty(Text))
            Text = Placeholder;
        return true;
    }

    [Export("textView:shouldChangeTextInRange:replacementText:")]
    public bool ShouldChangeText(UITextView textView, NSRange range, string text)
    {
        if (text.Contains("\n"))
        {
            this.EndEditing(true);
            return false;
        }
        return true;
    }
}

Note: You can not mix using ObjC/Swift style Delegates and C# anonymous Delegates, otherwise you end up with the error:

Event registration is overwriting existing delegate. Either just use events or your own delegate