Advertisement
Guest User

Untitled

a guest
Aug 10th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1.  
  2. package others;
  3.  
  4. import rallylive.com.R;
  5. import rallylive.com.R.styleable;
  6.  
  7. import android.content.Context;
  8. import android.content.res.AssetManager;
  9. import android.content.res.TypedArray;
  10. import android.graphics.Typeface;
  11. import android.util.AttributeSet;
  12. import android.widget.TextView;
  13.  
  14. import java.util.HashMap;
  15. import java.util.Map;
  16.  
  17. public class TypefaceTextView extends TextView {
  18.  
  19.     /*
  20.      * Caches typefaces based on their file path and name, so that they don't
  21.      * have to be created every time when they are referenced.
  22.      */
  23.     private static Map<String, Typeface> mTypefaces;
  24.  
  25.     public TypefaceTextView(final Context context) {
  26.         this(context, null);
  27.     }
  28.  
  29.     public TypefaceTextView(final Context context, final AttributeSet attrs) {
  30.         this(context, attrs, 0);
  31.     }
  32.  
  33.     public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
  34.         super(context, attrs, defStyle);
  35.         if (mTypefaces == null) {
  36.             mTypefaces = new HashMap<String, Typeface>();
  37.         }
  38.  
  39.         final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
  40.         if (array != null) {
  41.             final String typefaceAssetPath = array.getString(
  42.                     R.styleable.TypefaceTextView_customTypeface);
  43.  
  44.             if (typefaceAssetPath != null) {
  45.                 Typeface typeface = null;
  46.  
  47.                 if (mTypefaces.containsKey(typefaceAssetPath)) {
  48.                     typeface = mTypefaces.get(typefaceAssetPath);
  49.                 } else {
  50.                     AssetManager assets = context.getAssets();
  51.                     typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
  52.                     mTypefaces.put(typefaceAssetPath, typeface);
  53.                 }
  54.  
  55.                 setTypeface(typeface);
  56.             }
  57.             array.recycle();
  58.         }
  59.     }
  60.  
  61.     public void setCustomTypeface(Context context, String typefaceName){
  62.         Typeface font =     Typeface.createFromAsset(context.getAssets(), "fonts/" + typefaceName);
  63.         setTypeface(font);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement