Skip to content

Instantly share code, notes, and snippets.

@pcdus
Created February 14, 2018 16:50
Show Gist options
  • Save pcdus/e18507cbb4c97b8533f07670c0eddf96 to your computer and use it in GitHub Desktop.
Save pcdus/e18507cbb4c97b8533f07670c0eddf96 to your computer and use it in GitHub Desktop.
CircleButton control based on IconButton from Iconize
using Plugin.Iconize;
namespace IconizeCustomButton.Controls
{
public class CircleIconButton : IconButton
{
}
}
using Android.OS;
using IconizeCustomButton.Controls;
using IconizeCustomButton.Droid.Renderers;
using Plugin.Iconize;
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
/*
#if USE_FASTRENDERERS
using ButtonRenderer = Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer;
#else
using ButtonRenderer = Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer;
#endif
*/
[assembly: ExportRenderer(typeof(CircleIconButton), typeof(CircleIconButtonRenderer))]
namespace IconizeCustomButton.Droid.Renderers
{
/*
/// <summary>
/// Defines the <see cref="IconButtonRenderer" /> renderer.
/// </summary>
#if USE_FASTRENDERERS
/// <seealso cref="Xamarin.Forms.Platform.Android.FastRenderers.ButtonRenderer" />
#else
/// <seealso cref="Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer" />
#endif
*/
public class CircleIconButtonRenderer : ButtonRenderer
{
/*
public CircleIconButtonRenderer(Context context) : base(context)
{
this.SetWillNotDraw(false);
}
*/
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
#region Properties
/// <summary>
/// Gets the button.
/// </summary>
/// <value>
/// The button.
/// </value>
private IconButton Button => Element as IconButton;
#endregion Properties
/// <summary>
/// Called when [attached to window].
/// </summary>
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
#if USE_FASTRENDERERS
TextChanged += OnTextChanged;
#else
Control.TextChanged += OnTextChanged;
#endif
}
/// <summary>
/// Called when [detached from window].
/// </summary>
protected override void OnDetachedFromWindow()
{
#if USE_FASTRENDERERS
TextChanged -= OnTextChanged;
#else
Control.TextChanged -= OnTextChanged;
#endif
base.OnDetachedFromWindow();
}
/// <summary>
/// Raises the <see cref="E:ElementChanged" /> event.
/// </summary>
/// <param name="e">The <see cref="ElementChangedEventArgs{Button}" /> instance containing the event data.</param>
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Button == null)
return;
#if USE_FASTRENDERERS
SetAllCaps(false);
#else
Control.SetAllCaps(false);
#endif
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
this.SetBackground(null);
StateListAnimator = null;
}
UpdateText();
}
/// <summary>
/// Called when [element property changed].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="PropertyChangedEventArgs" /> instance containing the event data.</param>
protected override void OnElementPropertyChanged(Object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Button == null)
return;
switch (e.PropertyName)
{
case nameof(IconButton.FontSize):
case nameof(IconButton.TextColor):
UpdateText();
break;
}
}
/// <summary>
/// Called when [text changed].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="Android.Text.TextChangedEventArgs" /> instance containing the event data.</param>
private void OnTextChanged(Object sender, Android.Text.TextChangedEventArgs e)
{
UpdateText();
}
/// <summary>
/// Updates the text.
/// </summary>
private void UpdateText()
{
#if USE_FASTRENDERERS
TextChanged -= OnTextChanged;
#else
Control.TextChanged -= OnTextChanged;
#endif
var icon = Plugin.Iconize.Iconize.FindIconForKey(Button.Text);
if (icon != null)
{
#if USE_FASTRENDERERS
Text = $"{icon.Character}";
Typeface = Iconize.FindModuleOf(icon).ToTypeface(Context);
#else
Control.Text = $"{icon.Character}";
Control.Typeface = Plugin.Iconize.Iconize.FindModuleOf(icon).ToTypeface(Context);
#endif
}
#if USE_FASTRENDERERS
TextChanged += OnTextChanged;
#else
Control.TextChanged += OnTextChanged;
#endif
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:IconizeCustomButton"
xmlns:controls="clr-namespace:IconizeCustomButton.Controls"
xmlns:iconize="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"
x:Class="IconizeCustomButton.MainPage">
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Iconize-IconButton"
VerticalOptions="FillAndExpand" HorizontalOptions="Start"
HorizontalTextAlignment="Start" VerticalTextAlignment="Center"/>
<iconize:IconButton HeightRequest="40" WidthRequest="40"
BorderRadius="20"
Text="fa-500px" TextColor="Red" FontSize="20"
BackgroundColor="Orange" BorderColor="Red"
BorderWidth="2"
VerticalOptions="Start" HorizontalOptions="Center">
</iconize:IconButton>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Custom-CircleIconButton"
VerticalOptions="FillAndExpand" HorizontalOptions="Start"
HorizontalTextAlignment="Start" VerticalTextAlignment="Center"/>
<controls:CircleIconButton HeightRequest="40" WidthRequest="40"
BorderRadius="20"
Text="fa-500px" TextColor="Red" FontSize="20"
BackgroundColor="Orange" BorderColor="Red"
BorderWidth="2"
VerticalOptions="Start" HorizontalOptions="Center">
</controls:CircleIconButton>
</StackLayout>
</StackLayout>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment