react-native-advanced-text 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,7 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
25
25
  import android.text.Selection
26
26
  import android.graphics.Typeface
27
27
  import androidx.core.text.getSpans
28
+ import com.facebook.react.common.assets.ReactFontManager
28
29
 
29
30
  class AdvancedTextView : TextView {
30
31
 
@@ -163,9 +164,25 @@ class AdvancedTextView : TextView {
163
164
  fun setAdvancedFontFamily(family: String) {
164
165
  if (fontFamily == family) return
165
166
  fontFamily = family
166
- typeface = Typeface.create(family, Typeface.NORMAL)
167
+ typeface = resolveTypeface(family, Typeface.NORMAL)
167
168
  }
168
169
 
170
+ /**
171
+ * `Typeface.create(family, style)` only resolves OS-registered families; it
172
+ * silently substitutes a system font for anything bundled under
173
+ * `assets/fonts/` (the RN/Expo custom-font pipeline) instead of failing.
174
+ * The Fabric ShadowNode measures this component's text via
175
+ * `TextLayoutManager`, which resolves fonts through `ReactFontManager` -
176
+ * so a custom font here rendered with the substituted typeface has
177
+ * different glyph widths than what was measured, wrapping onto a
178
+ * different number of lines and producing extra height for some strings
179
+ * but not others. Going through `ReactFontManager` keeps the rendered
180
+ * typeface identical to the measured one (and is a no-op for system font
181
+ * names, which it falls back to `Typeface.create` for anyway).
182
+ */
183
+ private fun resolveTypeface(family: String, style: Int): Typeface =
184
+ ReactFontManager.getInstance().getTypeface(family, style, context.assets)
185
+
169
186
  fun setMenuOptions(menuOptions: List<String>) {
170
187
  if (this.menuOptions == menuOptions) return
171
188
  this.menuOptions = menuOptions
@@ -271,9 +288,9 @@ class AdvancedTextView : TextView {
271
288
  setTextSize(fontSize)
272
289
 
273
290
  typeface = when (fontWeight) {
274
- "bold" -> Typeface.create(fontFamily, Typeface.BOLD)
275
- "italic" -> Typeface.create(fontFamily, Typeface.ITALIC)
276
- else -> Typeface.create(fontFamily, Typeface.NORMAL)
291
+ "bold" -> resolveTypeface(fontFamily, Typeface.BOLD)
292
+ "italic" -> resolveTypeface(fontFamily, Typeface.ITALIC)
293
+ else -> resolveTypeface(fontFamily, Typeface.NORMAL)
277
294
  }
278
295
 
279
296
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-advanced-text",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": " Advanced text component for React Native with custom select options.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",