Browse Source

字体库使用工具类统一管理

liukai 2 years ago
parent
commit
99b261a9de

+ 6 - 6
cpt_ewallet/src/main/res/layout/fragment_ewallet_main.xml

@@ -265,13 +265,14 @@
                             android:layout_height="@dimen/d_26dp"
                             android:src="@drawable/wallet_password_icon" />
 
-                        <com.guadou.lib_baselib.font_text_view.TextViewLight
+                        <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:layout_marginLeft="@dimen/d_8dp"
                             android:text="支付密码设置"
                             android:textColor="@color/gray_76"
-                            android:textSize="@dimen/d_15sp" />
+                            android:textSize="@dimen/d_15sp"
+                            binding:typefaceRegular="@{true}" />
 
                         <View
                             android:layout_width="0dp"
@@ -307,13 +308,14 @@
                             android:layout_height="@dimen/d_25dp"
                             android:src="@drawable/wallet_history_icon" />
 
-                        <com.guadou.lib_baselib.font_text_view.TextViewMedium
+                        <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:layout_marginLeft="@dimen/d_8dp"
                             android:text="电子钱包交易记录"
                             android:textColor="@color/gray_76"
-                            android:textSize="@dimen/d_15sp" />
+                            android:textSize="@dimen/d_15sp"
+                            binding:typefaceRegular="@{true}" />
 
                         <View
                             android:layout_width="0dp"
@@ -353,7 +355,6 @@
                             android:layout_height="wrap_content"
                             android:layout_marginLeft="@dimen/d_8dp"
                             android:text="我的工作发票"
-                            binding:typefaceBold="@{true}"
                             android:textColor="@color/gray_76"
                             android:textSize="@dimen/d_15sp" />
 
@@ -395,5 +396,4 @@
 
     </LinearLayout>
 
-
 </layout>

+ 0 - 29
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/FontCache.java

@@ -1,29 +0,0 @@
-package com.guadou.lib_baselib.font_text_view;
-
-import android.content.Context;
-import android.graphics.Typeface;
-
-import java.util.HashMap;
-
-
-public class FontCache {
-
-    private static HashMap<String, Typeface> fontCache = new HashMap<>();
-
-    public static Typeface getTypeface(String fontname, Context context) {
-        Typeface typeface = fontCache.get(fontname);
-
-        if (typeface == null) {
-            try {
-                typeface = Typeface.createFromAsset(context.getAssets(), "SF-UI-Text/" + fontname);
-            } catch (Exception e) {
-                return null;
-            }
-
-            fontCache.put(fontname, typeface);
-        }
-
-        return typeface;
-    }
-
-}

+ 1 - 1
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/TextViewBold.java

@@ -28,6 +28,6 @@ public class TextViewBold extends AppCompatTextView {
     }
 
     private void applyCustomFont(Context context) {
-        setTypeface(Typeface.create("sans-serif", Typeface.BOLD));
+        setTypeface(TypefaceUtil.getBold());
     }
 }

+ 1 - 1
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/TextViewLight.java

@@ -29,6 +29,6 @@ public class TextViewLight extends AppCompatTextView {
     }
 
     private void applyCustomFont(Context context) {
-        setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
+        setTypeface(TypefaceUtil.getLight());
     }
 }

+ 1 - 2
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/TextViewMedium.java

@@ -1,7 +1,6 @@
 package com.guadou.lib_baselib.font_text_view;
 
 import android.content.Context;
-import android.graphics.Typeface;
 import android.util.AttributeSet;
 
 import androidx.annotation.Nullable;
@@ -29,6 +28,6 @@ public class TextViewMedium extends AppCompatTextView {
     }
 
     private void applyCustomFont(Context context) {
-        setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
+        setTypeface(TypefaceUtil.getMedium());
     }
 }

+ 1 - 2
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/TextViewRegular.java

@@ -1,7 +1,6 @@
 package com.guadou.lib_baselib.font_text_view;
 
 import android.content.Context;
-import android.graphics.Typeface;
 import android.util.AttributeSet;
 
 import androidx.annotation.Nullable;
@@ -29,6 +28,6 @@ public class TextViewRegular extends AppCompatTextView {
     }
 
     private void applyCustomFont(Context context) {
-        setTypeface(Typeface.create("sans-serif", Typeface.NORMAL));
+        setTypeface(TypefaceUtil.getRegular());
     }
 }

+ 15 - 10
cs_baselib/src/main/java/com/guadou/lib_baselib/font_text_view/TypefaceUtil.java

@@ -1,19 +1,24 @@
 package com.guadou.lib_baselib.font_text_view;
 
-/**
- * Created by C02TVKSDHV27 on 02/08/2017.
- */
+import android.graphics.Typeface;
 
 public class TypefaceUtil {
 
+    public static Typeface getLight() {
+        return Typeface.create("sans-serif-light", Typeface.NORMAL);
+    }
 
-//    public static Typeface getSFMedium(Context context) {
-//        return FontCache.getTypeface("PingFang_Medium.ttf", context);
-//    }
-//
-//    public static Typeface getSFBold(Context context) {
-//        return FontCache.getTypeface("PingFang_Bold.ttf", context);
-//    }
+    public static Typeface getRegular() {
+        return Typeface.create("sans-serif", Typeface.NORMAL);
+    }
+
+    public static Typeface getMedium() {
+        return Typeface.create("sans-serif-medium", Typeface.NORMAL);
+    }
+
+    public static Typeface getBold() {
+        return Typeface.create("sans-serif", Typeface.BOLD);
+    }
 
 
 }

+ 7 - 7
cs_cptServices/src/main/java/com/guadou/cs_cptservices/binding/BindingTextView.kt

@@ -1,13 +1,13 @@
 package com.guadou.cs_cptservices.binding
 
 import android.graphics.Paint
-import android.graphics.Typeface
 import android.graphics.drawable.Drawable
 import android.os.Build
 import android.text.Html
 import android.text.TextUtils
 import android.widget.TextView
 import androidx.databinding.BindingAdapter
+import com.guadou.lib_baselib.font_text_view.TypefaceUtil
 
 /**
  * 文本的设置
@@ -68,30 +68,30 @@ fun isCenterLine(textView: TextView, isUnderline: Boolean) {
  */
 @BindingAdapter("typefaceLight")
 fun typefaceLight(textView: TextView, boolean: Boolean) {
-    textView.typeface = Typeface.create("sans-serif-light", Typeface.NORMAL)
+    textView.typeface = TypefaceUtil.getLight()
 }
 
 @BindingAdapter("typefaceRegular")
 fun typefaceRegular(textView: TextView, boolean: Boolean) {
-    textView.typeface = Typeface.create("sans-serif", Typeface.NORMAL)
+    textView.typeface = TypefaceUtil.getRegular()
 }
 
 @BindingAdapter("typefaceMedium")
 fun typefaceMedium(textView: TextView, boolean: Boolean) {
-    textView.typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
+    textView.typeface = TypefaceUtil.getMedium()
 }
 
 @BindingAdapter("typefaceBold")
 fun typefaceSFBold(textView: TextView, boolean: Boolean) {
-    textView.typeface = Typeface.create("sans-serif", Typeface.BOLD)
+    textView.typeface = TypefaceUtil.getBold()
 }
 
 @BindingAdapter("typefaceMediumOrBold")
 fun typefaceMediumOrBold(textView: TextView, boolean: Boolean) {
     if (boolean) {  //true为粗体
-        textView.typeface = Typeface.create("sans-serif", Typeface.BOLD)
+        textView.typeface = TypefaceUtil.getBold()
     } else {
-        textView.typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
+        textView.typeface = TypefaceUtil.getMedium()
     }
 }