Aneesh.A.M
Aneesh.A.M

Reputation: 1148

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" />

Upvotes: 5

Views: 6782

Answers (2)

Diego Ven&#226;ncio
Diego Ven&#226;ncio

Reputation: 6007

No need more Customize, only need use in XAML: HorizontalTextAlignment="Center"

Upvotes: -1

Mario Galv&#225;n
Mario Galv&#225;n

Reputation: 4032

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;
        }
    }
}

Upvotes: 3

Related Questions