Skip to content

Instantly share code, notes, and snippets.

@Axemasta
Created June 3, 2021 17:28
Show Gist options
  • Save Axemasta/7a8ba685870d32f2418da63ec2c17a95 to your computer and use it in GitHub Desktop.
Save Axemasta/7a8ba685870d32f2418da63ec2c17a95 to your computer and use it in GitHub Desktop.
Entry CreateButtonColor Effect
public class AndroidClearButtonColorEffect : PlatformEffect
{
private EditText _textField;
private readonly Context _context;
ClearButtonColorEffect _formsEffect
{
get
{
return Element.GetFormsEffect<ClearButtonColorEffect>();
}
}
public AndroidClearButtonColorEffect()
{
_context = Xamarin.Essentials.Platform.CurrentActivity;
}
protected override void OnAttached()
{
var element = Control as EditText;
if (element is null) return;
_textField = element;
var closeDrawable = GetCloseButtonDrawable() as VectorDrawable;
if (closeDrawable is null) return;
var buttonColor = _formsEffect.Color.ToAndroid();
closeDrawable.SetTint(buttonColor);
}
private Drawable GetCloseButtonDrawable()
{
return ContextCompat.GetDrawable(_context, Resource.Drawable.abc_ic_clear_material);
}
protected override void OnDetached()
{
}
}
public class ClearButtonColorEffect : RoutingEffect
{
public Color Color { get; set; } = Color.Default;
public ClearButtonColorEffect()
: base ("Acme.Effects.ClearButtonColorEffect")
{
}
}
public class iOSClearButtonColorEffect : PlatformEffect
{
ClearButtonColorEffect _formsEffect
{
get
{
return Element.GetFormsEffect<ClearButtonColorEffect>();
}
}
private static NSString _clearButtonKey = new NSString("clearButton");
private UITextField _textField;
protected override void OnAttached()
{
var element = Control as UITextField;
if (element is null) return;
_textField = element;
var clearButton = _textField.ValueForKey(_clearButtonKey) as UIButton;
if (clearButton != null)
{
var color = _formsEffect.Color.ToUIColor();
var image = clearButton.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
clearButton.SetImage(image, UIControlState.Normal);
clearButton.TintColor = color;
}
}
protected override void OnDetached()
{
throw new NotImplementedException();
}
}
public static class EffectExtension
{
public static TEffect GetFormsEffect<TEffect>(this Element element)
where TEffect : Effect
{
if (element == null)
throw new ArgumentNullException("Forms element must not be null", nameof(element));
var effect = element.Effects.FirstOrDefault(e => e is TEffect);
return (TEffect)effect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment