Issue
Please Help me to make VerticalTextAlignment and HorisontalTextAlignment I use this Site but I don't know how to add VerticalTextAlignment
https://forums.xamarin.com/discussion/58774/button-horizontal-text-alignment
And this example did not work
How to change Text Alignment of button on Android (Xamarin Forms)?
I use 1 site and it is work please help to add VerticalTextAlignment to first code
public class ExtendedButton : Xamarin.Forms.Button
{
public static BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create<ExtendedButton, Xamarin.Forms.TextAlignment>(x => x.HorizontalTextAlignment, Xamarin.Forms.TextAlignment.Center);
public Xamarin.Forms.TextAlignment HorizontalTextAlignment
{
get
{
return (Xamarin.Forms.TextAlignment)GetValue(HorizontalTextAlignmentProperty);
}
set
{
SetValue(HorizontalTextAlignmentProperty, value);
}
}
}
Android
[assembly:ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer)]
namespace SomeNamespace.Droid
{
public class ExtendedButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
{
public new ExtendedButton Element
{
get
{
return (ExtendedButton)base.Element;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
{
return;
}
SetTextAlignment();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ExtendedButton.HorizontalTextAlignmentProperty.PropertyName)
{
SetTextAlignment();
}
}
public void SetTextAlignment()
{
Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags();
}
}
public static class AlignmentHelper
{
public static GravityFlags ToHorizontalGravityFlags(this Xamarin.Forms.TextAlignment alignment)
{
if (alignment == Xamarin.Forms.TextAlignment.Center)
return GravityFlags.AxisSpecified;
return alignment == Xamarin.Forms.TextAlignment.End ? GravityFlags.Right : GravityFlags.Left;
}
}
}
Solution
You also can use this way to add VerticalTextAlignmentProperty
for ExtendedButton as follows:
public class ExtendedButton:Button
{
public static BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create<ExtendedButton, Xamarin.Forms.TextAlignment>(x => x.HorizontalTextAlignment, Xamarin.Forms.TextAlignment.Center);
public Xamarin.Forms.TextAlignment HorizontalTextAlignment
{
get
{
return (Xamarin.Forms.TextAlignment)GetValue(HorizontalTextAlignmentProperty);
}
set
{
SetValue(HorizontalTextAlignmentProperty, value);
}
}
public static BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create<ExtendedButton, Xamarin.Forms.TextAlignment>(x => x.VerticalTextAlignment, Xamarin.Forms.TextAlignment.Center);
public Xamarin.Forms.TextAlignment VerticalTextAlignment
{
get
{
return (Xamarin.Forms.TextAlignment)GetValue(VerticalTextAlignmentProperty);
}
set
{
SetValue(VerticalTextAlignmentProperty, value);
}
}
}
Then ExtendedButtonRenderer modify as follows:
[assembly: ExportRenderer(typeof(ExtendedButton), typeof(ExtendedButtonRenderer))]
namespace XamrinTemp.Droid
{
[Obsolete]
public class ExtendedButtonRenderer : ButtonRenderer
{
public new ExtendedButton Element
{
get
{
return (ExtendedButton)base.Element;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
{
return;
}
SetHorizonalTextAlignment();
SetVerticalTextAlignment();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ExtendedButton.HorizontalTextAlignmentProperty.PropertyName)
{
SetHorizonalTextAlignment();
}
if (e.PropertyName == ExtendedButton.VerticalTextAlignmentProperty.PropertyName)
{
SetVerticalTextAlignment();
}
}
private void SetHorizonalTextAlignment()
{
Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags() | Element.VerticalTextAlignment.ToVerticalGravityFlags();
}
private void SetVerticalTextAlignment()
{
Control.Gravity = Element.VerticalTextAlignment.ToVerticalGravityFlags() | Element.HorizontalTextAlignment.ToHorizontalGravityFlags();
}
}
public static class AlignmentHelper
{
public static GravityFlags ToHorizontalGravityFlags(this Xamarin.Forms.TextAlignment alignment)
{
if (alignment == Xamarin.Forms.TextAlignment.Center)
return GravityFlags.CenterHorizontal;
return alignment == Xamarin.Forms.TextAlignment.End ? GravityFlags.Right : GravityFlags.Left;
}
public static GravityFlags ToVerticalGravityFlags(this Xamarin.Forms.TextAlignment alignment)
{
if (alignment == Xamarin.Forms.TextAlignment.Center)
return GravityFlags.CenterVertical;
return alignment == Xamarin.Forms.TextAlignment.End ? GravityFlags.Top : GravityFlags.Bottom;
}
}
}
Now if settting property in Xaml as follows:
<local:ExtendedButton Text="Here is the Text of Extended Button"
WidthRequest="300"
HeightRequest="200"
HorizontalTextAlignment="Start"
VerticalTextAlignment="End"/>
The effect will show:
Answered By - Junior Jiang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.