How to set Placeholder and PlaceHolder Color in Editor Xamarin Forms

前端 未结 2 1927
孤街浪徒
孤街浪徒 2021-01-21 02:21

How to set Placeholder Text and Placeholder Color in Editor in Xamarin Forms.

It has no default Functiona

相关标签:
2条回答
  • 2021-01-21 02:46

    Set the placeholder string to your Editor's Text in Xaml Then in Code behind file:

    InitializeComponent();
    
    var placeholder = myEditor.Text;
    
    myEditor.Focused += (sender, e) =>
    {
         // Set the editor's text empty on focus, only if the place 
         // holder is present
         if (myEditor.Text.Equals(placeholder))
         {
              myEditor.Text = string.Empty;
              // Here You can change the text color of editor as well
              // to active text color
         }
    };
    
    myEditor.Unfocused += (sender, e) => 
    {
         // Set the editor's text to place holder on unfocus, only if 
         // there is no data entered in editor
        if (string.IsNullOrEmpty(myEditor.Text.Trim()))
        {
            myEditor.Text = placeholder;
            // Here You can change the text color of editor as well
            // to dim text color (Text Hint Color)
        }
    };
    
    0 讨论(0)
  • 2021-01-21 02:55

    You will need a custom renderer for that (Here is the Android Custom Renderer) you will need another renderer for iOS:

    public class PlaceholderEditor : Editor
    {
        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create<PlaceholderEditor, string>(view => view.Placeholder, String.Empty);
    
        public PlaceholderEditor()
        {
        }
    
        public string Placeholder
        {
            get
            {
                return (string)GetValue(PlaceholderProperty);
            }
    
            set
            {
                SetValue(PlaceholderProperty, value);
            }
        }
    }
    
    public class PlaceholderEditorRenderer : EditorRenderer
    {
        public PlaceholderEditorRenderer()
        {
        }
    
        protected override void OnElementChanged(
            ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
    
            if (e.NewElement != null)
            {
                var element = e.NewElement as PlaceholderEditor;
                this.Control.Hint = element.Placeholder;
            }
        }
    
        protected override void OnElementPropertyChanged(
            object sender,
            PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (e.PropertyName == PlaceholderEditor.PlaceholderProperty.PropertyName)
            {
                var element = this.Element as PlaceholderEditor;
                this.Control.Hint = element.Placeholder;
            }
        }
    }
    

    And for the color you may need something like this (Android):

    Control.SetHintTextColor(Android.Graphics.Color.White);
    

    There already a thread for this in the Xamarin Forums: https://forums.xamarin.com/discussion/20616/placeholder-editor

    And more about Custom Renderers information below: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

    0 讨论(0)
提交回复
热议问题