Back to project page RealTextView.
The source code is released under:
Apache License
If you think the Android project RealTextView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.hardsoftstudio.real.textview.utils; /*w w w. java2s . c o m*/ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; import com.hardsoftstudio.real.textview.R; import java.util.HashMap; import java.util.Map; public class FontManager { private static FontManager sInstance; private Map<String, Typeface> mCache; private static int sDefaultStyle = R.attr.fontifyStyle; private FontManager() { mCache = new HashMap<String, Typeface>(); } public static FontManager getInstance() { if (sInstance == null) { sInstance = new FontManager(); } return sInstance; } public void setFont(TextView tv, AttributeSet attrs) { String fontName = getFontName(tv.getContext(), attrs); setFont(tv, fontName); } public void setFont(TextView tv, String fontName) { if (fontName == null) return; Typeface typeface = mCache.get(fontName); if (typeface == null) { typeface = Typeface.createFromAsset(tv.getContext().getAssets(), fontName); mCache.put(fontName, typeface); } tv.setTypeface(typeface); } public static String getFontName(Context c, AttributeSet attrs) { TypedArray arr = c.obtainStyledAttributes( attrs, R.styleable.RealTextView, sDefaultStyle, 0 ); String fontName = arr.getString(R.styleable.RealTextView_font); arr.recycle(); return fontName; } }