Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder text alignment in Xamarin forms

I don't know how to top the placeholder text in a entry box. I have a entry box that is very big and want to put the placeholder text in the top.

<Entry Placeholder="Enter notes about the item" 
       Keyboard="Text" VerticalOptions="Start" HeightRequest="80" />
like image 326
Aneesh.A.M Avatar asked Oct 19 '22 01:10

Aneesh.A.M


1 Answers

You will need to create a custom renderer for each platform to aling the placeholder something like this:

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;
        }
    }
}
like image 108
Mario Galván Avatar answered Jan 04 '23 06:01

Mario Galván