0
votes

In Xamarin Forms I tried to add show password feature to entry. so I toggled isPassword(true/false). user has to click on the eye icon to see the password and it means that entry will lose focus. Now, In iOs if user touches entry to enter more character or delete some. iOs will clear the entry and user has to enter again. Is there a way to disable this?

1

1 Answers

1
votes

We can Custom EntryRenderer to avoid that . Such as creating a CustomEntryRenderer in project.iOS solution :

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace AppEntryTest.iOS
{
    public class CustomEntryRenderer : EntryRenderer,IUITextFieldDelegate
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control.SecureTextEntry)
            {
               Control.Delegate = new MyTextFiledDelegate();
            }
            
        }

    }

    internal class MyTextFiledDelegate : UITextFieldDelegate
    {
        public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
        {
            //return base.ShouldChangeCharacters(textField, range, replacementString);
            string updatedString = textField.Text.Substring(0, (int)range.Location)+replacementString+textField.Text.Substring((int)(range.Location+range.Length));
            textField.Text = updatedString;
            return false;
        }
    }
}

Used in Xaml :

<StackLayout VerticalOptions="FillAndExpand" Padding="20">
    <Entry Text="First Entry" IsPassword="True"/>
    <Entry Placeholder="Second Entry"/>
</StackLayout>

The effect :

enter image description here