Is it possible to switch off autocorrect on iOS custom forms elements

DeniseGrovesDeniseGroves USMember ✭✭
edited December 2015 in Xamarin.Forms

I have been asked to disable autocorrect on an iOS form view and can't work out how to do it. The XAML looks like this:

< local:ValidatedEntry x:Name="FirstName"
Header="First name"
InvalidText="The text you have entered is too long or not provided"
HeaderFont="{x:Static local:StyleConstants.FormFieldTitleFont}"
Text=""
ValidMode="MinMax" Min="1" Max="30" CapitaliseText="true" >

I tried setting the autocorrectionType property to UITextAutocorrectionType.No, but, presumably because the ValidatedEntry class inherits from ContentView, this doesn't work. I can't find much information on this on line - does anyone know how to switch off autocorrect on custom elements?

Thanks

Denise

Posts

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Hi @DeniseGroves

    You seem to have forgotten your XAML code :)

    I have written a CustomRenderer for this once which looks like this;

    [assembly: ExportRenderer(typeof(NoAutocorrectEntry), typeof(NoAutocorrectEntryRenderer))]
    
    namespace App.iOS.CustomRenderers
    {
        public class NoAutocorrectEntryRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                var textField = (UITextField)Control;
    
                // No auto-correct
                textField.AutocorrectionType = UITextAutocorrectionType.No;
                textField.SpellCheckingType = UITextSpellCheckingType.No;
                textField.AutocapitalizationType = UITextAutocapitalizationType.Words;
            }
        }
    }
    
  • DeniseGrovesDeniseGroves USMember ✭✭

    Thanks Gerald. I'm not sure I can parse a control that inherits from ContentView to a UITextField though, can I?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Well you have to have an input field inside your control somewhere. Traverse the subviews property and find the UITextField or UITextView that is there and then edit its properties.

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    @DeniseGroves said:
    Thanks Gerald. I'm not sure I can parse a control that inherits from ContentView to a UITextField though, can I?

    What does your ValidatedEntry look like? like @AdamMeaney says there probably is a UITextView in there somewhere on which you can apply the renderer.

  • DeniseGrovesDeniseGroves USMember ✭✭

    Thanks for the input. I should have mentioned that I am very new to Xamarin and have been asked to take over an existing project developed by a third party

    I've done some digging around... the custom control ValidatedEntry contains an Entry control rather than a UITextView or UITextField. I assume this is because it is a cross-platform app. It looks as though the Entry class doesn't have any properties related to autocomplete. Does that sound right? Or do you think I can assign a Keyboard type to get around it?

  • PatrickSpainPatrickSpain USMember ✭✭

    @DeniseGroves said:
    FYI I set the following and it seems to work :smiley:

    textEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence | KeyboardFlags.Spellcheck);

    Wow, this works. But can you explain why it works. It seems weird that using the bit OR turns it off? Usually you would do this if you want two different enums to take effect. Is there some configuration is both of these are set to not do? Doesn't make sense, but the fact that this hides the keyboard also doesn't make sense to me.

  • KeithDorkenKeithDorken CAMember

    @PatrickSpain said:

    @DeniseGroves said:
    FYI I set the following and it seems to work :smiley:

    textEntry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeSentence | KeyboardFlags.Spellcheck);

    Wow, this works. But can you explain why it works. It seems weird that using the bit OR turns it off? Usually you would do this if you want two different enums to take effect. Is there some configuration is both of these are set to not do? Doesn't make sense, but the fact that this hides the keyboard also doesn't make sense to me.

    She wanted to disable only the AutoCorrect, but keep the other features. She is specifying the features she wants.

  • AbdulbashaAbdulbasha USMember ✭✭

    @DeniseGroves thanks a lot. Even its working for Searchbar as well.

Sign In or Register to comment.