14
14
import android .content .res .AssetManager ;
15
15
import android .graphics .Typeface ;
16
16
import android .os .Build ;
17
+ import android .util .Pair ;
17
18
import android .util .SparseArray ;
18
19
19
20
import androidx .annotation .NonNull ;
@@ -41,7 +42,7 @@ public class ReactFontManager {
41
42
private static ReactFontManager sReactFontManagerInstance ;
42
43
43
44
final private Map <String , FontFamily > mFontCache ;
44
- final private Map <String , Typeface > mCustomTypefaceCache ;
45
+ final private Map <Pair < String , Integer > , Typeface > mCustomTypefaceCache ;
45
46
46
47
private ReactFontManager () {
47
48
mFontCache = new HashMap <>();
@@ -67,12 +68,18 @@ public static ReactFontManager getInstance() {
67
68
int style ,
68
69
int weight ,
69
70
AssetManager assetManager ) {
70
- if (mCustomTypefaceCache .containsKey (fontFamilyName )) {
71
- Typeface typeface = mCustomTypefaceCache .get (fontFamilyName );
72
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P && weight >= 100 && weight <= 1000 ) {
73
- return Typeface .create (typeface , weight , (style & Typeface .ITALIC ) != 0 );
71
+ Pair key = Pair .create (fontFamilyName , weight );
72
+ if (mCustomTypefaceCache .containsKey (key )) {
73
+ return Typeface .create (mCustomTypefaceCache .get (key ), style );
74
+ } else {
75
+ key = Pair .create (fontFamilyName , null );
76
+ if (mCustomTypefaceCache .containsKey (key )) {
77
+ Typeface typeface = mCustomTypefaceCache .get (key );
78
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P && weight >= 100 && weight <= 1000 ) {
79
+ return Typeface .create (typeface , weight , (style & Typeface .ITALIC ) != 0 );
80
+ }
81
+ return Typeface .create (typeface , style );
74
82
}
75
- return Typeface .create (typeface , style );
76
83
}
77
84
78
85
FontFamily fontFamily = mFontCache .get (fontFamilyName );
@@ -106,8 +113,28 @@ public void addCustomFont(@NonNull Context context, @NonNull String fontFamily,
106
113
}
107
114
}
108
115
109
- public void addCustomFont (@ NonNull String fontFamily , @ NonNull Typeface font ) {
110
- mCustomTypefaceCache .put (fontFamily , font );
116
+ /*
117
+ * This method allows you to load custom fonts from a custom Typeface object and register it as a specific
118
+ * fontFamily and weight. This can be used when fonts are delivered during runtime and cannot be included in
119
+ * the standard app resources. Typeface's registered using a specific weight will take priority over ones
120
+ * registered without a specific weight.
121
+ *
122
+ * ReactFontManager.getInstance().addCustomFont("Srisakdi", 600, typeface);
123
+ */
124
+ public void addCustomFont (@ NonNull String fontFamily , int weight , @ NonNull Typeface typeface ) {
125
+ mCustomTypefaceCache .put (Pair .create (fontFamily , weight ), typeface );
126
+ }
127
+
128
+ /*
129
+ * This method allows you to load custom fonts from a custom Typeface object and register it as a specific
130
+ * fontFamily. This can be used when fonts are delivered during runtime and cannot be included in
131
+ * the standard app resources. Typeface's registered using a specific weight will take priority over ones
132
+ * registered without a specific weight.
133
+ *
134
+ * ReactFontManager.getInstance().addCustomFont("Srisakdi", typeface);
135
+ */
136
+ public void addCustomFont (@ NonNull String fontFamily , @ NonNull Typeface typeface ) {
137
+ mCustomTypefaceCache .put (Pair .create (fontFamily , null ), typeface );
111
138
}
112
139
113
140
/**
0 commit comments