How to display custom password character with some delay like UITextfield control

EswaranEswaran INMember ✭✭

I have created a custom control by inheriting the UITextField control. My requirements are to display the custom password character and when typing there should be some delay like UITextField when setting the SecureTextEntry as true. I have achieved the custom password character displaying but I am unable to achieve the second requirement. I tried to achieve the second requirement by using DispatchQueue. When typing a single character I can have some delay before it changes to password character but when typing multiple characters fastly I cant able to achieve the requirement similar to SecureTextEntry. I have attached the sample in the below link: https://github.com/Eswaran17392/CustomSamples. Anyone, please suggest me how to achieve the delay similar to SecureTextEntry.

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    I modified your Handle_ShouldChangeCharacters like:

    NSTimer timer;
    private bool Handle_ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
    {
        if (timer != null)
        {
            timer.Invalidate();
            timer = null;
            ChangeToPasswordChar();
        }           
    
        NSString originalText = new NSString(textField.Text);
        textField.Text = originalText.Replace(range, new NSString(replacementString));
    
        timer = NSTimer.CreateScheduledTimer(1, false, (t) =>
        {
            ChangeToPasswordChar();
        });
    
        return false;
    }
    
    public void ChangeToPasswordChar()
    {
        data = string.Empty;
        for (int i = 0; i < this.Text.Length; i++)
        {
            data += this.PasswordChar;
        }
        this.Text = data;
    }
    

    I only offer the code when the ShowPasswordDelay has been set to true. You can add other conditions.

Answers

  • EswaranEswaran INMember ✭✭

    The password delay can be achieved by using SecureTextEntry but in my case, it cannot be used since I am trying to display custom password character. So I tried to achieve the second requirement by using DispatchQueue as given in the below code snippet.

    var popuptTime = new DispatchTime(DispatchTime.Now, 1000000000);
    DispatchQueue.MainQueue.DispatchAfter(
    popuptTime,
    () =>
    {
    this.Text = value;
    }

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    I modified your Handle_ShouldChangeCharacters like:

    NSTimer timer;
    private bool Handle_ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
    {
        if (timer != null)
        {
            timer.Invalidate();
            timer = null;
            ChangeToPasswordChar();
        }           
    
        NSString originalText = new NSString(textField.Text);
        textField.Text = originalText.Replace(range, new NSString(replacementString));
    
        timer = NSTimer.CreateScheduledTimer(1, false, (t) =>
        {
            ChangeToPasswordChar();
        });
    
        return false;
    }
    
    public void ChangeToPasswordChar()
    {
        data = string.Empty;
        for (int i = 0; i < this.Text.Length; i++)
        {
            data += this.PasswordChar;
        }
        this.Text = data;
    }
    

    I only offer the code when the ShowPasswordDelay has been set to true. You can add other conditions.

  • EswaranEswaran INMember ✭✭

    Hi @LandLu ,

    Your solution worked as expected. Thank you so much :) .

  • EswaranEswaran INMember ✭✭

    Hi @LandLu ,

    With the above solution, I achieved all my requirements with UITextfield control in xamarin iOS platform. How can I achieve the same requirement with EditText control in Xamarin android platform?. Please suggest any possible solution.

Sign In or Register to comment.