select Typeface - Android Graphics

Android examples for Graphics:Font

Description

select Typeface

Demo Code


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class Main{
    private static Typeface selectTypeface(Context context,
            String fontName, int textStyle) {
        if (fontName.contentEquals(context
                .getString(R.string.font_name_fontawesome))) {
            return FontCache.getTypeface("fontawesome.ttf", context);
        } else if (fontName.contentEquals(context
                .getString(R.string.font_name_source_sans_pro))) {
            /*//from   www  .j a va  2 s . c  om
            information about the TextView textStyle:
            http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle
             */
            switch (textStyle) {
            case Typeface.BOLD: // bold
                return FontCache.getTypeface("SourceSansPro-Bold.ttf",
                        context);

            case Typeface.ITALIC: // italic
                return FontCache.getTypeface("SourceSansPro-Italic.ttf",
                        context);

            case Typeface.BOLD_ITALIC: // bold italic
                return FontCache.getTypeface(
                        "SourceSansPro-BoldItalic.ttf", context);

            case 10: // extra light, equals @integer/font_style_extra_light
                return FontCache.getTypeface(
                        "SourceSansPro-ExtraLight.ttf", context);

            case 11: // extra bold, equals @integer/font_style_extra_bold
                return FontCache.getTypeface("SourceSansPro-Black.ttf",
                        context);

            case Typeface.NORMAL: // regular
            default:
                return FontCache.getTypeface("SourceSansPro-Regular.ttf",
                        context);
            }
        } else {
            // no matching font found
            // return null so Android just uses the standard font (Roboto)
            return null;
        }
    }
}

Related Tutorials