Eventos

[Xamarin] ¿Cómo usar Segoe MDL2 Assets en Xamarin.Android?

En esta ocasión quiero compartir como hago para poder usar las fuentes Segoe MDL2 en un proyecto Xamarin.Android.

segmdl2.ttf

Agregamos la fuente y establecemos su Build Action como AndroidAsset.


Typefaces.cs

Creamos la clase que nos permita construir la Typeface en base a la fuente que acabamos de agregar.

using Android.App;
using Android.Graphics;
namespace Blog.Epicalsoft.Com.Droid
{
public class SegoeMDL2Typeface
{
public static Typeface Instance = Typeface.CreateFromAsset(Application.Context.Assets, "segmdl2.ttf");
private SegoeMDL2Typeface()
{
}
}
}
view raw Typefaces.cs hosted with ❤ by GitHub

IconTextView.cs

Y ahora extendemos TextView para personalizarlo con el uso de la Typeface que acabamos de obtener.

using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
namespace Blog.Epicalsoft.Com.Droid.Views
{
public class IconTextView : TextView
{
public IconTextView(Context context) : base(context)
{
Typeface = SegoeMDL2Typeface.Instance;
Gravity = Android.Views.GravityFlags.CenterVertical;
}
public IconTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
Typeface = SegoeMDL2Typeface.Instance;
Gravity = Android.Views.GravityFlags.CenterVertical;
}
public IconTextView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
Typeface = SegoeMDL2Typeface.Instance;
Gravity = Android.Views.GravityFlags.CenterVertical;
}
protected IconTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
Typeface = SegoeMDL2Typeface.Instance;
Gravity = Android.Views.GravityFlags.CenterVertical;
}
}
}
view raw IconTextView.cs hosted with ❤ by GitHub

Layout.axml

Una vez hecho esto ya esta todo listo para usar nuestro IconTextView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<blog.epicalsoft.com.droid.views.IconTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="\uE001 " />
</LinearLayout>
view raw Layout.axml hosted with ❤ by GitHub

No hay comentarios.:

Publicar un comentario

Epicalsoft — Superheroic Software Development Blog Designed by Templateism Copyright © 2014

Con tecnología de Blogger.