Quellcode durchsuchen

[공통][Bug] 폰트 적용
- assets 의 font 파일을 Typeface 로 변환하여 reflection 을 이용하여 폰트를 덮어 씌운다.

hyodong.min vor 7 Jahren
Ursprung
Commit
231ed41782

BIN
app/src/main/assets/font/droid_sans.ttf


BIN
app/src/main/assets/font/mbyeonsung.otf


+ 4 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/application/App.java

@@ -7,6 +7,7 @@ import android.content.Context;
 import android.support.multidex.MultiDexApplication;
 
 import kr.co.zumo.app.BuildConfig;
+import kr.co.zumo.app.lifeplus.supervisor.TypefaceUtil;
 import kr.co.zumo.app.lifeplus.util.Logg;
 
 /**
@@ -34,8 +35,10 @@ public class App extends MultiDexApplication {
 
     Logg.w("APP# App | onCreate", "|" + "===============================================================================================");
     Logg.w("APP# App | onCreate", "| << debug: " + BuildConfig.DEBUG + ", appId: " + BuildConfig.APPLICATION_ID
-      + ", buildType: " + BuildConfig.BUILD_TYPE + ", flavor: " + BuildConfig.FLAVOR + ", versionCode: " + BuildConfig.VERSION_CODE + ", versionName: "+ BuildConfig.VERSION_NAME + " >>");
+      + ", buildType: " + BuildConfig.BUILD_TYPE + ", flavor: " + BuildConfig.FLAVOR + ", versionCode: " + BuildConfig.VERSION_CODE + ", versionName: " + BuildConfig.VERSION_NAME + " >>");
     Logg.w("APP# App | onCreate", "|" + "===============================================================================================");
+
+    new TypefaceUtil().init(getContext());
   }
 
   /**

+ 105 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/supervisor/TypefaceUtil.java

@@ -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();
+    }
+  }
+}

+ 12 - 0
app/src/main/res/font/font2.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<font-family xmlns:android="http://schemas.android.com/apk/res/android"
+             xmlns:app="http://schemas.android.com/apk/res-auto">
+  <font
+    android:font="@string/lifeplus_font2"
+    android:fontStyle="normal"
+    android:fontWeight="700"
+    app:font="@string/lifeplus_font2"
+    app:fontStyle="normal"
+    app:fontWeight="700"/>
+
+</font-family>

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -4,6 +4,9 @@
   ]>
 
 <resources>
+  <string name="lifeplus_font1" translatable="false">lifeplus_font1</string>
+  <string name="lifeplus_font2" translatable="false">lifeplus_font2</string>
+
   <string name="empty_string" translatable="false"/>
   <string name="confirm">확인</string>
   <string name="faq">자주찾는 질문</string>

+ 8 - 24
app/src/main/res/values/styles.xml

@@ -1,25 +1,9 @@
 <resources>
 
-
   <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
     <item name="android:windowBackground">@drawable/splash_background</item>
   </style>
 
-  <!-- Base application theme. -->
-  <!--  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
-
-      &lt;!&ndash; Customize your theme here. &ndash;&gt;
-      <item name="colorPrimary">@color/colorPrimary</item>
-      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
-      <item name="colorAccent">@color/colorAccent</item>
-    </style>-->
-
-  <style name="CustomDialog" parent="@style/Theme.AppCompat.Light">
-    <item name="android:windowNoTitle">false</item>
-    <item name="android:windowFullscreen">true</item>
-    <item name="android:windowIsFloating">false</item>
-  </style>
-
   <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
@@ -28,26 +12,21 @@
   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     <!-- Customize your theme here. -->
     <item name="actionBarSize">57dp</item>
-    <!--<item name="android:background">@color/CFFFFFF</item>-->
     <item name="colorPrimary">@color/CFFFFFF</item>
     <item name="colorPrimaryDark">@color/C000000</item>
     <item name="colorAccent">@color/C000000</item>
   </style>
 
-  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
-
-  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
   <style name="FullScreenDialog" parent="Theme.AppCompat.Light.Dialog">
     <item name="android:backgroundDimEnabled">false</item>
     <item name="android:windowNoTitle">true</item>
     <item name="android:padding">0dp</item>
     <item name="android:windowIsFloating">false</item>
     <item name="android:windowBackground">@android:color/transparent</item>
-    <!--<item name="android:fontFamily">@font/regular</item>-->
+    <item name="android:fontFamily">@string/lifeplus_font2</item>
   </style>
 
-
-  <style name="SignUpButton" parent="Widget.AppCompat.Button.Borderless">
+  <style name="SignUpButton" parent="@android:style/Widget.DeviceDefault.Button.Borderless">
     <item name="android:layout_width">wrap_content</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:minHeight">38dp</item>
@@ -61,9 +40,10 @@
     <item name="android:layout_marginStart">2dp</item>
     <item name="android:layout_marginEnd">2dp</item>
     <item name="android:textAllCaps">false</item>
+    <item name="android:fontFamily">@string/lifeplus_font2</item>
   </style>
 
-  <style name="PinButton" parent="Widget.AppCompat.Button.Borderless">
+  <style name="PinButton" parent="@android:style/Widget.DeviceDefault.Button.Borderless">
     <item name="android:layout_width">wrap_content</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:minHeight">38dp</item>
@@ -79,4 +59,8 @@
     <item name="android:gravity">center</item>
     <item name="android:lineSpacingExtra">9sp</item>
   </style>
+
+  <style name="font2">
+    <item name="android:fontFamily">@string/lifeplus_font2</item>
+  </style>
 </resources>