Skip to content

Instantly share code, notes, and snippets.

@softlion
Last active April 18, 2024 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softlion/6a11b0ec5acb384743500ffaf92d525e to your computer and use it in GitHub Desktop.
Save softlion/6a11b0ec5acb384743500ffaf92d525e to your computer and use it in GitHub Desktop.
Button with HorizontalTextAlignmentProperty and VerticalTextAlignmentProperty
using Microsoft.Maui.Handlers;
#if ANDROID
using Google.Android.Material.Button;
#elif IOS
using UIKit;
#elif WINDOWS
using Microsoft.UI.Xaml.Controls;
#endif
namespace RxUI.MauiToolkit.Controls;
public static class RxButtonExtensions
{
public static MauiAppBuilder UseRxButton(this MauiAppBuilder builder)
{
return builder.ConfigureMauiHandlers(
handlers =>
{
#if ANDROID
handlers.AddHandler<RxButton, RxButtonHandler>();
#elif IOS
handlers.AddHandler<RxButton, RxButtonHandler>();
#elif WINDOWS
handlers.AddHandler<RxButton, RxButtonHandler>();
#endif
});
}
}
/// <summary>
/// https://github.com/marcoablanco/RxUI.MauiToolkit/blob/develop/RxUI.MauiToolkit/Controls/RxButton.cs
/// https://github.com/marcoablanco/RxUI.MauiToolkit/blob/develop/RxUI.MauiToolkit/Platforms/Android/Handlers/RxButtonHandler.cs
/// https://github.com/marcoablanco/RxUI.MauiToolkit/blob/develop/RxUI.MauiToolkit/Platforms/Windows/Handlers/RxButtonHandler.cs
/// https://github.com/marcoablanco/RxUI.MauiToolkit/blob/develop/RxUI.MauiToolkit/Platforms/iOS/Handlers/RxButtonHandler.cs
/// </summary>
public class RxButton : Button
{
public static readonly BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create(nameof(HorizontalTextAlignment), typeof(TextAlignment), typeof(RxButton));
public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create(nameof(VerticalTextAlignment), typeof(TextAlignment), typeof(RxButton));
public TextAlignment HorizontalTextAlignment
{
get => (TextAlignment)GetValue(HorizontalTextAlignmentProperty);
set => SetValue(HorizontalTextAlignmentProperty, value);
}
public TextAlignment VerticalTextAlignment
{
get => (TextAlignment)GetValue(VerticalTextAlignmentProperty);
set => SetValue(VerticalTextAlignmentProperty, value);
}
}
internal class RxButtonHandler : ButtonHandler
{
public override void UpdateValue(string property)
{
base.UpdateValue(property);
if (property == RxButton.HorizontalTextAlignmentProperty.PropertyName)
OnTextAlignmentPropertyChanged();
}
#if ANDROID
protected override void ConnectHandler(MaterialButton platformView)
{
base.ConnectHandler(platformView);
OnTextAlignmentPropertyChanged();
}
#elif IOS
protected override void ConnectHandler(UIButton platformView)
{
base.ConnectHandler(platformView);
OnTextAlignmentPropertyChanged();
}
#endif
private void OnTextAlignmentPropertyChanged()
{
if (VirtualView is RxButton virtualButton)
{
#if ANDROID
var horizontalFlag = virtualButton.HorizontalTextAlignment switch
{
TextAlignment.Start => global::Android.Views.GravityFlags.Left,
TextAlignment.Center => global::Android.Views.GravityFlags.CenterHorizontal,
TextAlignment.End => global::Android.Views.GravityFlags.Right,
_ => global::Android.Views.GravityFlags.Center
};
var verticalFlag = virtualButton.VerticalTextAlignment switch
{
TextAlignment.Start => global::Android.Views.GravityFlags.Top,
TextAlignment.Center => global::Android.Views.GravityFlags.CenterVertical,
TextAlignment.End => global::Android.Views.GravityFlags.Bottom,
_ => global::Android.Views.GravityFlags.Center
};
PlatformView.Gravity = horizontalFlag | verticalFlag;
#elif IOS
// PlatformView.HorizontalAlignment = virtualButton.HorizontalTextAlignment switch
// {
// TextAlignment.Start => UIKit.UIControlContentHorizontalAlignment.Left,
// TextAlignment.Center => UIKit.UIControlContentHorizontalAlignment.Center,
// TextAlignment.End => UIKit.UIControlContentHorizontalAlignment.Right,
// _ => UIKit.UIControlContentHorizontalAlignment.Center,
// };
PlatformView.TitleLabel.TextAlignment = virtualButton.HorizontalTextAlignment switch
{
TextAlignment.Start => UITextAlignment.Left,
TextAlignment.Center => UITextAlignment.Center,
TextAlignment.End => UITextAlignment.Right,
_ => UITextAlignment.Center,
};
PlatformView.VerticalAlignment = virtualButton.HorizontalTextAlignment switch
{
TextAlignment.Start => UIKit.UIControlContentVerticalAlignment.Top,
TextAlignment.Center => UIKit.UIControlContentVerticalAlignment.Center,
TextAlignment.End => UIKit.UIControlContentVerticalAlignment.Bottom,
_ => UIKit.UIControlContentVerticalAlignment.Center,
};
#elif WINDOWS
PlatformView.HorizontalContentAlignment = virtualButton.HorizontalTextAlignment switch
{
TextAlignment.Start => Microsoft.UI.Xaml.HorizontalAlignment.Left,
TextAlignment.Center => Microsoft.UI.Xaml.HorizontalAlignment.Center,
TextAlignment.End => Microsoft.UI.Xaml.HorizontalAlignment.Right,
_ => Microsoft.UI.Xaml.HorizontalAlignment.Center,
};
PlatformView.VerticalContentAlignment = virtualButton.HorizontalTextAlignment switch
{
TextAlignment.Start => Microsoft.UI.Xaml.VerticalAlignment.Top,
TextAlignment.Center => Microsoft.UI.Xaml.VerticalAlignment.Center,
TextAlignment.End => Microsoft.UI.Xaml.VerticalAlignment.Bottom,
_ => Microsoft.UI.Xaml.VerticalAlignment.Center,
};
#endif
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment