|
|
@@ -0,0 +1,105 @@
|
|
|
+/*
|
|
|
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
|
|
|
+ */
|
|
|
+package kr.co.zumo.app.lifeplus.supervisor;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.graphics.Typeface;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import kr.co.zumo.app.R;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 앱에서 사용할 폰트 설정
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * TypefaceUtil
|
|
|
+ * <pre>
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author 민효동
|
|
|
+ * @version 1.0
|
|
|
+ * @history 민효동 [2018. 10. 8.] [최초 작성]
|
|
|
+ * @since 2018. 10. 8.
|
|
|
+ */
|
|
|
+
|
|
|
+public class TypefaceUtil {
|
|
|
+ private static final String REGULAR_FONT_ASSET_NAME = "font/droid_sans.ttf"; // in assets folder
|
|
|
+ private static final String MEDIUM_FONT_ASSET_NAME = "font/mbyeonsung.otf"; // in assets folder
|
|
|
+
|
|
|
+ // private static final String REGULAR_FONT_FILE = "/system/fonts/droid_sans.ttf"; // 사용 가능하지만 없는 유저도 있을 수 있어서 asset 에 임베딩
|
|
|
+
|
|
|
+ private static final String FONT_DEFAULT = "DEFAULT";
|
|
|
+
|
|
|
+ public void init(Context context) {
|
|
|
+ try {
|
|
|
+ Typeface regular = createFont(context, REGULAR_FONT_ASSET_NAME);
|
|
|
+ Typeface medium = createFont(context, MEDIUM_FONT_ASSET_NAME);
|
|
|
+
|
|
|
+ Map<String, Typeface> fonts = new HashMap<>();
|
|
|
+ fonts.put(context.getString(R.string.lifeplus_font1), regular);
|
|
|
+ fonts.put(context.getString(R.string.lifeplus_font2), medium);
|
|
|
+ overrideFonts(fonts);
|
|
|
+
|
|
|
+ overrideFont(FONT_DEFAULT, regular);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * assets 폴더의 font 를 가져온다.
|
|
|
+ *
|
|
|
+ * @param context Context
|
|
|
+ * @param path assets 을 제외한 경로
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Typeface createFont(Context context, String path) {
|
|
|
+ return Typeface.createFromAsset(context.getAssets(), path);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * font 파일을 가져온다.
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Typeface createFromFile(String path) {
|
|
|
+ return Typeface.createFromFile(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void overrideFonts(Map<String, Typeface> typefaces) {
|
|
|
+ try {
|
|
|
+ final Field field = Typeface.class.getDeclaredField("sSystemFontMap");
|
|
|
+ field.setAccessible(true);
|
|
|
+
|
|
|
+ Map<?, ?> tempMap = (Map<?, ?>) field.get(null);
|
|
|
+ Map<String, Typeface> oldFonts = new HashMap<>();
|
|
|
+
|
|
|
+ for (Map.Entry<?, ?> entry : tempMap.entrySet()) {
|
|
|
+ oldFonts.put((String) entry.getKey(), (Typeface) entry.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ oldFonts.putAll(typefaces);
|
|
|
+ field.set(null, oldFonts);
|
|
|
+ field.setAccessible(false);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void overrideFont(String fontName, Typeface typeface) {
|
|
|
+ try {
|
|
|
+ final Field field = Typeface.class.getDeclaredField(fontName);
|
|
|
+ field.setAccessible(true);
|
|
|
+ field.set(null, typeface);
|
|
|
+ field.setAccessible(false);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|