Skip to content

Commit ba1b2e9

Browse files
authored
Add support for registering custom fonts by family+weight (#179)
* Add support for registering custom fonts by family+weight
1 parent 4af7d60 commit ba1b2e9

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

.ado/publish.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ function doPublish() {
128128
}
129129

130130
console.log("Created GitHub Release: " + JSON.stringify(body, null, 2));
131+
if (body.id) {
131132
uploadReleaseAssetUrl = assetUpdateUrl.replace(/{id}/, body.id);
132133
uploadTarBallToRelease();
134+
} else {
135+
console.warn('Unable to find release to upload tar...skipping custom tar for release.')
136+
}
133137
}
134138
);
135139
}

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.content.res.AssetManager;
1515
import android.graphics.Typeface;
1616
import android.os.Build;
17+
import android.util.Pair;
1718
import android.util.SparseArray;
1819

1920
import androidx.annotation.NonNull;
@@ -41,7 +42,7 @@ public class ReactFontManager {
4142
private static ReactFontManager sReactFontManagerInstance;
4243

4344
final private Map<String, FontFamily> mFontCache;
44-
final private Map<String, Typeface> mCustomTypefaceCache;
45+
final private Map<Pair<String, Integer>, Typeface> mCustomTypefaceCache;
4546

4647
private ReactFontManager() {
4748
mFontCache = new HashMap<>();
@@ -67,12 +68,18 @@ public static ReactFontManager getInstance() {
6768
int style,
6869
int weight,
6970
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);
7482
}
75-
return Typeface.create(typeface, style);
7683
}
7784

7885
FontFamily fontFamily = mFontCache.get(fontFamilyName);
@@ -106,8 +113,28 @@ public void addCustomFont(@NonNull Context context, @NonNull String fontFamily,
106113
}
107114
}
108115

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);
111138
}
112139

113140
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native",
3-
"version": "0.60.0-microsoft.8",
3+
"version": "0.60.0-microsoft.9",
44
"description": "[Microsoft Fork] A framework for building native apps using React",
55
"license": "MIT",
66
"repository": {

0 commit comments

Comments
 (0)