Skip to content

Instantly share code, notes, and snippets.

@steveevers
Created December 28, 2017 00:37
Show Gist options
  • Save steveevers/21bd9bfd6b5d9ae9290946da2f9f143c to your computer and use it in GitHub Desktop.
Save steveevers/21bd9bfd6b5d9ae9290946da2f9f143c to your computer and use it in GitHub Desktop.
Custom versions of Button, EditText, and TextView for Xamarin.Android that support custom fonts
<resources>
<declare-styleable name="Fonts">
<attr name="android:textStyle" />
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>
using System;
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Widget;
namespace MyProject.Droid.Controls
{
public class CustomButton : Button
{
public CustomButton(Context context)
: this(context, null)
{
}
public CustomButton(Context context, IAttributeSet attributes)
: this(context, attributes, Resource.Attribute.titleTextStyle)
{
}
public CustomButton(Context context, IAttributeSet attributes, int defStyle)
: base(context, attributes, defStyle)
{
var a = context.ObtainStyledAttributes(attributes, Resource.Styleable.Fonts);
var font = a.GetString(Resource.Styleable.Fonts_customFont);
this.SetCustomFont(font);
a.Recycle();
}
public void SetCustomFont(string asset)
{
Typeface typeface = null;
try
{
typeface = Typeface.CreateFromAsset(Context.Assets, asset);
if (typeface == null)
{
Console.WriteLine("No typeface found for asset: " + asset);
return;
}
}
catch
{
Console.WriteLine("Could not load typeface from asset: " + asset);
return;
}
var style = typeface?.Style ?? TypefaceStyle.Normal;
this.SetTypeface(typeface, style);
}
}
}
using System;
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Widget;
namespace MyProject.Droid.Controls
{
public class CustomEditText : EditText
{
public CustomEditText(Context context)
: this(context, null)
{
}
public CustomEditText(Context context, IAttributeSet attributes)
: this(context, attributes, Resource.Attribute.titleTextStyle)
{
}
public CustomEditText(Context context, IAttributeSet attributes, int defStyle)
: base(context, attributes, defStyle)
{
var a = context.ObtainStyledAttributes(attributes, Resource.Styleable.Fonts);
var font = a.GetString(Resource.Styleable.Fonts_customFont);
this.SetBackgroundColorFilter(context);
this.SetCustomFont(font);
a.Recycle();
}
private void SetBackgroundColorFilter(Context context)
{
//this.Background.SetColorFilter(context.Resources.GetColor(Resource.Color.appBorderColor), PorterDuff.Mode.SrcAtop);
}
public void SetCustomFont(string asset)
{
Typeface typeface = null;
try
{
typeface = Typeface.CreateFromAsset(Context.Assets, asset);
if (typeface == null)
{
Console.WriteLine("No typeface found for asset: " + asset);
return;
}
}
catch
{
Console.WriteLine("Could not load typeface from asset: " + asset);
return;
}
var style = typeface?.Style ?? TypefaceStyle.Normal;
this.SetTypeface(typeface, style);
}
}
}
using System;
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Widget;
namespace MyProject.Droid.Controls
{
public class CustomTextView : TextView
{
public CustomTextView(Context context)
: this(context, null)
{
}
public CustomTextView(Context context, IAttributeSet attributes)
: this(context, attributes, Resource.Attribute.titleTextStyle)
{
}
public CustomTextView(Context context, IAttributeSet attributes, int defStyle)
: base(context, attributes, defStyle)
{
var a = context.ObtainStyledAttributes(attributes, Resource.Styleable.Fonts);
var font = a.GetString(Resource.Styleable.Fonts_customFont);
this.SetCustomFont(font);
a.Recycle();
}
public void SetCustomFont(string asset)
{
Typeface typeface = null;
try
{
typeface = Typeface.CreateFromAsset(Context.Assets, asset);
if (typeface == null)
{
Console.WriteLine("No typeface found for asset: " + asset);
return;
}
}
catch
{
Console.WriteLine("Could not load typeface from asset: " + asset);
return;
}
var style = typeface?.Style ?? TypefaceStyle.Normal;
this.SetTypeface(typeface, style);
}
}
}
<MyProject.Droid.Controls.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
customFont="Fonts/Font.otf"
android:text="Click Me!" />
<MyProject.Droid.Controls.CustomEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
customFont="Fonts/Font.otf"
android:text="Click Me!" />
<MyProject.Droid.Controls.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
customFont="Fonts/Font.otf"
android:text="Click Me!" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment