Xamarin.Plugins
Xamarin.Plugins copied to clipboard
IconButton with Text
I needed this an option so quickly hacked this together with meets my needs, feel free to use or destroy. I have not done a iOS version but people should get the idea
extendediconbutton.cs
using Xamarin.Forms;
namespace testproject
{
/// <summary>
/// Defines the <see cref="ExtendedIconButton" /> control.
/// </summary>
/// <seealso cref="Xamarin.Forms.Button" />
public class ExtendedIconButton : Button
{
public static readonly BindableProperty IconProperty =
BindableProperty.Create("Icon", typeof(string), typeof(ExtendedIconButton), "");
public string Icon
{
get { return (string)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
}
}
extendediconbuttonrenderer.cs
using System;
using System.ComponentModel;
using FormsPlugin.Iconize;
using Plugin.Iconize.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using ButtonRenderer = Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer;
using testproject;
using testproject.Droid;
[assembly: ExportRenderer(typeof(ExtendedIconButton), typeof(ExtendedIconButtonRenderer))]
namespace testproject.Droid
{
/// <summary>
/// Defines the <see cref="IconButtonRenderer" /> renderer.
/// </summary>
/// <seealso cref="Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer" />
public class ExtendedIconButtonRenderer : ButtonRenderer
{
/// <summary>
/// Gets the underlying control typed as an <see cref="ImageButton"/>.
/// </summary>
private ExtendedIconButton ImageButton
{
get { return (ExtendedIconButton)Element; }
}
/// <summary>
/// Called when [attached to window].
/// </summary>
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
Control.TextChanged += OnTextChanged;
}
/// <summary>
/// Called when [detached from window].
/// </summary>
protected override void OnDetachedFromWindow()
{
Control.TextChanged -= OnTextChanged;
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 (Control == null || Element == null)
return;
Control.SetAllCaps(false);
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 (Control == null || Element == 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()
{
Control.TextChanged -= OnTextChanged;
var icon = Plugin.Iconize.Iconize.FindIconForKey(ImageButton.Icon);
if (icon != null)
{
Control.Text = $"{icon.Character} " + ImageButton.Text;
Control.Typeface = Plugin.Iconize.Iconize.FindModuleOf(icon).ToTypeface(Context);
}
Control.TextChanged += OnTextChanged;
}
}
}`