Example usage for android.graphics Typeface createFromAsset

List of usage examples for android.graphics Typeface createFromAsset

Introduction

In this page you can find the example usage for android.graphics Typeface createFromAsset.

Prototype

public static Typeface createFromAsset(AssetManager mgr, String path) 

Source Link

Document

Create a new typeface from the specified font data.

Usage

From source file:Main.java

/**
 * Creates Roboto typeface and puts it into cache
 *//*from  w  w  w. j a  va  2s  .c  o  m*/
private static Typeface getRobotoTypeface(Context context, String fontType, Typeface typeface) {
    String fontPath = "fonts/" + getFontName(fontType, typeface) + ".ttf";

    if (!typefaceCache.containsKey(fontType)) {
        typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath));
    }

    return typefaceCache.get(fontType);
}

From source file:Main.java

/** Retrieve a type-face. Does not load twice, uses lazy loading. */
public static Typeface getTypeface(Context context, String name) {
    // ensure the global context is used. just in case.
    context = context.getApplicationContext();
    Log.d(TAG, "name=" + name);
    if (TYPEFACE_CACHE.containsKey(name)) {
        return TYPEFACE_CACHE.get(name);
    }//  w  w w  . j  av a 2s . c  om

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), name);

    if (typeface != null) {
        TYPEFACE_CACHE.put(name, typeface);
    }

    return typeface;
}

From source file:Main.java

/**
 * Sets a determined font on a text view element
 *
 * @param context   Context in which the TextView can be found
 * @param font      Font to be set in the text view see available fonts as static attributes of this class
 * @param style     {@see Typeface}//from   w  w w  .j  a v a 2 s .  c o  m
 * @param textViews TextViews to which the font will be applied
 */
public static void setTypeface(Context context, String font, int style, TextView... textViews) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), font);
    for (TextView txt : textViews) {
        txt.setTypeface(tf, style);
    }
}

From source file:Main.java

public static Typeface getFontAwesomeTypeFace(Context context) {

    return Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
}

From source file:Main.java

public static Typeface getTypeface(Context context) {
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(), "Helvetica.ttf");
    }//from  ww  w. ja v a 2s.c  o m
    return typeface;
}

From source file:Main.java

public static Typeface getBoldTypeface(Context context) {
    if (typefaceBold == null) {
        typefaceBold = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
                "Helvetica_Bold.ttf");
    }/*www . j a  va 2 s.  c  o m*/
    return typefaceBold;
}

From source file:Main.java

/**
 * Returns typeface font from assets. If cached returns the cached value, otherwise loads from
 * assets and caches it.//  w ww.  j av a 2 s .  co m
 * 
 * @param context
 * @param fontName
 * @return
 */
public static final Typeface getCachedFont(Context context, String fontName) {
    Typeface tf = null;
    Context appContext = context.getApplicationContext();
    try {
        // check if the font is cached
        if (mCachedFonts.containsKey(fontName) && mCachedFonts.get(fontName) != null) {
            tf = mCachedFonts.get(fontName);
        } else {
            // get the font from assets
            tf = Typeface.createFromAsset(appContext.getAssets(), "fonts/" + fontName);
            // cache the font
            mCachedFonts.put(fontName, tf);
        }
    } catch (Exception e) {
    }
    return tf;
}

From source file:Main.java

public static Typeface getCustomTypeFace(Context context) {
    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/regular.otf");

    return tf;/*  w  ww .j  a  v a2s.c  om*/
}

From source file:Main.java

public static Typeface get(Context context, String font) {
    synchronized (sTypefaceCache) {
        if (!sTypefaceCache.containsKey(font)) {
            Typeface tf = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
                    "fonts/" + font + ".ttf");
            sTypefaceCache.put(font, tf);
        }/* w ww . j a v a  2  s.c om*/
        return sTypefaceCache.get(font);
    }
}

From source file:Main.java

public static void setCustomFont(View topView, AssetManager assetsManager) {
    if (normal == null || bold == null || condensed == null || light == null) {
        normal = Typeface.createFromAsset(assetsManager, "fonts/segoeui.ttf");
        bold = Typeface.createFromAsset(assetsManager, "fonts/segouib.ttf");
        condensed = Typeface.createFromAsset(assetsManager, "fonts/segoeui.ttf");
        light = Typeface.createFromAsset(assetsManager, "fonts/segoeui;.ttf");
    }/*w  w w  . j  a  v  a 2  s  . c  o  m*/

    if (topView instanceof ViewGroup) {
        setCustomFont((ViewGroup) topView);
    } else if (topView instanceof TextView) {
        setCustomFont((TextView) topView);
    }
}