Android Open Source - RealTextView Font Manager






From Project

Back to project page RealTextView.

License

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.

Java Source Code

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;
  }
}




Java Source Code List

com.hardsoftstudio.real.textview.exceptions.RealHtmlTextViewException.java
com.hardsoftstudio.real.textview.utils.AutofitHelper.java
com.hardsoftstudio.real.textview.utils.FontManager.java
com.hardsoftstudio.real.textview.utils.HtmlTagHandler.java
com.hardsoftstudio.real.textview.utils.LocalImageGetter.java
com.hardsoftstudio.real.textview.utils.RealUrl.java
com.hardsoftstudio.real.textview.utils.SizeUtils.java
com.hardsoftstudio.real.textview.utils.UrlImageGetter.java
com.hardsoftstudio.real.textview.views.BaseTextView.java
com.hardsoftstudio.real.textview.views.RealButton.java
com.hardsoftstudio.real.textview.views.RealCheckBox.java
com.hardsoftstudio.real.textview.views.RealEditText.java
com.hardsoftstudio.real.textview.views.RealTextView.java
org.hardsoftstudio.real.example.ApplicationTest.java
org.hardsoftstudio.real.example.BaseActivity.java
org.hardsoftstudio.real.example.ExampleMainActivity.java
org.hardsoftstudio.real.example.HtmlExampleActivity.java
org.hardsoftstudio.real.example.MainActivity.java
org.hardsoftstudio.real.textview.ApplicationTest.java