locked
How to programmatically set the color of Placeholder text of a disabled Entry on iOS RRS feed

  • Question

  • User89714 posted

    Can anybody tell me please how to specify the color of the placeholder for an Entry that is disabled on iOS? (Android and UWP are sorted).

    I know I can workaround it by placing another View over the top of the Entry, but it would be useful to do it "properly" in a custom Entry renderer.

    Saturday, August 4, 2018 4:47 PM

All replies

  • User369978 posted

    You can implement this by custom renderer.

    [assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
    namespace FormsApp.iOS
    {
        class CustomEntryRenderer :EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                   if(Control.Enabled == false)
                    {
                        Control.AttributedPlaceholder = new NSAttributedString(Control.Placeholder, new UIStringAttributes { ForegroundColor = UIColor.Red});
                    }
                }
            }
        }
    }
    
    Monday, August 6, 2018 6:29 AM
  • User89714 posted

    Thanks @ColeX . I already had that AttributedPlaceholder code in place, both in OnElementChanged and in OnElementPropertyChanged. There turned out to be two issues:

    (1) A copy-and-paste error on my part - I was calling the base class in OnElementPropertyChanged after setting the color instead of before. (2) Changing IsEnabled to false overrode the placeholder color that I had previously set, so I also needed to set the placeholder color each time IsEnabled changed.

    Too many late night coding sessions I think - simple mistakes and omissions :-(

    Monday, August 6, 2018 12:30 PM
  • User369978 posted

    When you enable or disable the entry ,OnElementPropertyChanged method can be triggered every time, so we can move the logic into OnElementPropertyChanged .

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (Control != null)
            {
                if (Control.Enabled == false)  //disable status
                {
                    Control.AttributedPlaceholder =
                        new NSAttributedString(Control.Placeholder, new UIStringAttributes { ForegroundColor = UIColor.Red });
                }
                else  ////enable status
                {
                    Control.AttributedPlaceholder =
                        new NSAttributedString(Control.Placeholder, new UIStringAttributes { ForegroundColor = UIColor.Gray });
                }
            }
        }
    
    Tuesday, August 7, 2018 5:38 AM