網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
React Native App 在部分型號(hào)的 Android 手機(jī)上,可能會(huì)發(fā)生文字顯示不全的問題。
官方也有一個(gè)?相關(guān)的 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], }) }
但是升級(jí) React Native 到 0.66 之后,上面這個(gè)方法就不起作用了。
復(fù)現(xiàn)問題
作者在 React Native 0.67.4 環(huán)境下,編寫了一個(gè)小 demo 來復(fù)現(xiàn)這個(gè)問題,如下:
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", }, })
當(dāng)?Text
?中的文本同時(shí)符合以下兩個(gè)條件時(shí),在部分 Android 手機(jī)上會(huì)出現(xiàn)文字顯示不全的問題。
fontWeight
譬如作者這臺(tái)手機(jī):
手機(jī)型號(hào) | MIUI 版本 | Android 版本 |
---|---|---|
MI 8 | 12.5.2 | 10 |
就會(huì)出現(xiàn)文字顯示不全的問題,即使將?fontWeight
?設(shè)置為?bold
?,也不會(huì)有粗體效果。
但是只要 style 設(shè)置了?fontFamily
?,就不會(huì)有顯示不全的問題,因此,怎樣才能正確地設(shè)置?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 ) }
示例
這里有?一個(gè)示例?,供你參考。
原文鏈接:https://juejin.cn/post/7127811778620162078
相關(guān)推薦
- 2022-04-16 spyder快捷鍵與python符號(hào)化輸出方式_python
- 2022-03-18 docker容器啟動(dòng)設(shè)置固定IP的實(shí)現(xiàn)_docker
- 2022-12-25 終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法_python
- 2022-03-30 C#算法之回文數(shù)_C#教程
- 2023-07-08 編寫socket配置文件
- 2022-10-01 React?hooks?useState異步問題及解決_React
- 2021-12-13 C語(yǔ)言數(shù)組學(xué)習(xí)之特殊矩陣的壓縮存儲(chǔ)_C 語(yǔ)言
- 2022-03-15 spark-submit hive SQL standards based authorizati
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支