Skip to content

Instantly share code, notes, and snippets.

@kaka2507
Created September 22, 2016 10:30
Show Gist options
  • Save kaka2507/c8e5f9524464ae232fcbcb498f8e36d8 to your computer and use it in GitHub Desktop.
Save kaka2507/c8e5f9524464ae232fcbcb498f8e36d8 to your computer and use it in GitHub Desktop.
package mm.com.mypay.consumer.utils;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import mm.com.mypay.consumer.uis.views.CustomFontTextView;
import mm.com.mypay.consumer.R;
import mm.com.mypay.consumer.uis.activities.BaseActivity;
/**
* Created by vcoder on 9/22/16.
*/
public class CustomFontUtils {
public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public static void applyCustomFont(CustomFontTextView customFontTextView, Context context, AttributeSet attrs) {
AppLog.d("applyCustonFont");
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomFontTextView);
String fontName = attributeArray.getString(R.styleable.CustomFontTextView_font);
// check if a special textStyle was used (e.g. extra bold)
int textStyle = attributeArray.getInt(R.styleable.CustomFontTextView_textStyle, 0);
// if nothing extra was used, fall back to regular android:textStyle parameter
if (textStyle == 0) {
textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
}
Typeface customFont = selectTypeface(context, fontName, textStyle);
customFontTextView.setTypeface(customFont);
attributeArray.recycle();
}
private static Typeface selectTypeface(Context context, String fontName, int textStyle) {
AppLog.d("selectTypeface");
if(LocaleHelper.get().getLocale().contentEquals(LocaleHelper.ENG)) {
// For English version
switch (textStyle) {
case Typeface.BOLD: // bold
return FontCache.getTypeface("Roboto-Bold.ttf", context);
case Typeface.ITALIC: // italic
return FontCache.getTypeface("Roboto-Italic.ttf", context);
case Typeface.BOLD_ITALIC:
return FontCache.getTypeface("Roboto-BoldItalic.ttf", context);
case Typeface.NORMAL:
default:
return FontCache.getTypeface("Roboto-Regular.ttf", context);
}
} else {
// For Myanmar version
return FontCache.getTypeface("Zawgyi_One.ttf", context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment