Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text alignment on a picker xamarin forms

i am having trouble with aligning the text on an expanded picker to the centre and i would have thought it would be something like this...

enter image description here

and here is the picker on the emulator...

enter image description here

How would i get that SIZE text to sit in the middle but keep the line expanded also can it be done through the xaml code instead of c# code

cheers.

like image 744
Getwrong Avatar asked Dec 20 '17 07:12

Getwrong


People also ask

How can we set alignment of text?

Align the text left or right Select the text that you want to align. On the Home tab, in the Paragraph group, click Align Left or Align Right .

How do I change the picker text color in xamarin?

You can use TextColor and TitleColor to change the font color and HeightRequest to change the size.

What is picker in xamarin forms?

Picker control is a view control for picking an element in a list. The visual representation of a Picker control is similar to an entry control, but a Picker control appears in place of a keyboard.


2 Answers

UPDATE***

Actually is very easy:

<Picker Title="Selecione o bairro" HorizontalTextAlignment="Center"/> 

...OLD

You need to use a CustomRenderer for this.

ANDROID

[assembly: ExportRenderer(typeof(BetterPicker), typeof(BetterPickerRenderer))]

namespace MDWS.Droid
{
    public class BetterPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.Gravity = GravityFlags.CenterHorizontal;
            }
        }
    }
}

IOS:

[assembly: ExportRenderer(typeof(BetterPicker), typeof(BetterPickerRenderer))]

namespace YourNameSpace.iOS
{
    public class BetterPickerRenderer : PickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.TextAlignment = UITextAlignment.Center;
            }
        }
    }
}
like image 146
Diego Venâncio Avatar answered Oct 02 '22 16:10

Diego Venâncio


I am afraid you will have to implement a CustomRenderer for this.
For each platform OnElementChanged you will have to set the alignment for Picker's title.

Luckily this topic is very nicely explained in official guide.

P.S.: Welcome to stackoverflow.com, please take a moment to read this post which nicely explains why posting a screenshot of code is a bad practice.

Good luck!

like image 25
EvZ Avatar answered Oct 02 '22 16:10

EvZ