網站首頁 編程語言 正文
React Native App 在部分型號的 Android 手機上,可能會發生文字顯示不全的問題。
官方也有一個?相關的 Issue?,并提供了如下的解決方案:
const defaultFontFamily = { ...Platform.select({ android: { fontFamily: "" }, }), } const oldRender = Text.render Text.render = function (...args) { const origin = oldRender.call(this, ...args) return React.cloneElement(origin, { style: [defaultFontFamily, origin.props.style], }) }
但是升級 React Native 到 0.66 之后,上面這個方法就不起作用了。
復現問題
作者在 React Native 0.67.4 環境下,編寫了一個小 demo 來復現這個問題,如下:
function IncompleteText() { return ( <View style={styles.container}> <View style={styles.row}> <Text style={styles.text}>我在左邊 完整</Text> <Text style={styles.text}>我在右邊 完整</Text> </View> <View style={styles.row}> <Text style={styles.text}>12345</Text> <Text style={styles.text}>67890</Text> </View> <View style={styles.row}> <Text style={styles.text}>abcd</Text> <Text style={styles.text}>efgh</Text> </View> <View style={styles.row}> <Text style={styles.text}>今年是 2022 年</Text> <Text style={styles.text}>虧了好多 ¥</Text> </View> </View> ) } const styles = StyleSheet.create({ container: { flex: 1, }, row: { marginTop: 16, marginHorizontal: 24, flexDirection: "row", alignItems: "center", justifyContent: "space-between", height: 60, backgroundColor: "#f5f5f5", }, text: { fontSize: 18, color: "#333333", fontWeight: "normal", // fontFamily: 'DFWaWaSC-W5', backgroundColor: "yellow", }, })
當?Text
?中的文本同時符合以下兩個條件時,在部分 Android 手機上會出現文字顯示不全的問題。
fontWeight
譬如作者這臺手機:
手機型號 | MIUI 版本 | Android 版本 |
---|---|---|
MI 8 | 12.5.2 | 10 |
就會出現文字顯示不全的問題,即使將?fontWeight
?設置為?bold
?,也不會有粗體效果。
但是只要 style 設置了?fontFamily
?,就不會有顯示不全的問題,因此,怎樣才能正確地設置?fontFamily
?呢?
解決問題
stack overflow 上,有人問?How to set default font family in React Native??。
在該問題的討論列表中,作者找到了適合 React Native 0.66 以上版本的解決方案,如下:
// text-polyfill.ts import React from "react" import { Platform, StyleSheet, Text, TextProps } from "react-native" const defaultFontFamily = { ...Platform.select({ android: { fontFamily: "" }, }), } // @ts-ignore const __render: any = Text.render // @ts-ignore Text.render = function (props: TextProps, ref: React.RefObject<Text>) { if (Platform.OS === "ios") { return __render.call(this, props, ref) } const { style, ..._props } = props const _style = StyleSheet.flatten(style) || {} return __render.call( this, { ..._props, style: { ...defaultFontFamily, ..._style } }, ref ) }
示例
這里有?一個示例?,供你參考。
原文鏈接:https://juejin.cn/post/7127811778620162078
相關推薦
- 2022-07-27 Python中range函數的使用方法_python
- 2022-11-18 Redux模塊化拆分reducer函數流程介紹_React
- 2022-04-02 Python操作word文檔的示例詳解_python
- 2023-03-23 詳解python?ThreadPoolExecutor異常捕獲_python
- 2022-08-25 利用Android實現光影流動特效的方法詳解_Android
- 2022-07-22 SpringBoot允許跨域訪問配置
- 2022-04-17 spring cloud config和bus組件實現自動刷新
- 2022-05-10 原生ajax 在服務器響應前撤銷請求
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支