Skip to content

Instantly share code, notes, and snippets.

@dkudelko
Last active March 2, 2016 13:00
Show Gist options
  • Save dkudelko/75d1e9378f6428da5e20 to your computer and use it in GitHub Desktop.
Save dkudelko/75d1e9378f6428da5e20 to your computer and use it in GitHub Desktop.
Xamarin Forms Android Custom Font
using Xamarin.Forms;
//https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/
[assembly: ExportRenderer(typeof(Label), typeof(CustomFontLabelRenderer))]
namespace dkudelko.Mobile.Android
{
public class CustomFontLabelRenderer : LabelRenderer
{
private static readonly string[] CustomFontFamily = new [] { "FontAwesome" };
private static readonly Tuple<FontAttributes, string>[][] CustomFontFamilyData = new [] {
new [] {
new Tuple<FontAttributes, string>(FontAttributes.None, "fontawesome-webfont.ttf"),
new Tuple<FontAttributes, string>(FontAttributes.Bold, "fontawesome-webfont.ttf"),
new Tuple<FontAttributes, string>(FontAttributes.Italic, "fontawesome-webfont.ttf"),
},
};
protected override bool CheckIfCustomFont (string fontFamily, FontAttributes attributes, out string fontFileName)
{
for (int i = 0; i < CustomFontFamily.Length; i++) {
if (string.Equals(fontFamily, CustomFontFamily[i], StringComparison.InvariantCulture)){
var fontFamilyData = CustomFontFamilyData[i];
for (int j = 0; j < fontFamilyData.Length; j++) {
var data = fontFamilyData[j];
if (data.Item1 == attributes){
fontFileName = data.Item2;
return true;
}
}
break;
}
}
fontFileName = null;
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment