Search code examples
xamarin.formsplaceholder

How to change the Text color of placeholder in Xamarin forms


Is it possible to change the Text color of part of the placeholder.

<Controls:BorderlessEntry Grid.Row="1" Grid.Column="0"
                          x:Name="ABS_ID"
                          Placeholder="Enter Name Here *"/>

I want to change the asterisk "*" Color to red and rest of the text will have default color.

Any help is appreciated!


Solution

  • You can try the following workaround:

    Wrap an Entry and a Label into a StackLayout. And make the StackLayout like an Entry with placeholder.

    The code is like this:

    MainPage.xaml:

    <StackLayout Orientation="Horizontal" HorizontalOptions="Center"
                 VerticalOptions="CenterAndExpand">
        <Entry x:Name="ABS_ID" 
               Text="Enter Name Here     "
               HorizontalOptions="Start" 
               Focused="OnEntryFocused" 
               Unfocused="OnEntryUnFocused"/>
        <Label x:Name="ABS_ID2" 
               Text="*" 
               TextColor="Red" 
               HorizontalOptions="StartAndExpand"  
               VerticalTextAlignment="Center" 
               Margin="-20"/>
    </StackLayout>
    

    MainPage.xaml.cs:

    public partial class MainPage : ContentPage
    {
        bool showPlaceHolder = true;
        public MainPage()
        {
            InitializeComponent();
        }
        void OnEntryFocused(object sender, EventArgs e)
        {
            if (showPlaceHolder) {
                ABS_ID.Text = "";
                ABS_ID2.Text = "";
            }
        }
        void OnEntryUnFocused(object sender, EventArgs e)
        {
            if ((ABS_ID.Text == "") && (ABS_ID2.Text == ""))
            {
                ABS_ID.Text = "Enter Name Here ";
                ABS_ID2.Text = "*";
                showPlaceHolder = true;
            }
            else {
                showPlaceHolder = false;
            }
        }
    }
    

    And the result is:

    screenshot of the result