React?Native?中處理?Android?手機(jī)吞字的解決方案
React Native App 在部分型號的 Android 手機(jī)上,可能會發(fā)生文字顯示不全的問題。
官方也有一個 相關(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], }) }
但是升級 React Native 到 0.66 之后,上面這個方法就不起作用了。
復(fù)現(xiàn)問題
作者在 React Native 0.67.4 環(huán)境下,編寫了一個小 demo 來復(fù)現(xiàn)這個問題,如下:
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
中的文本同時符合以下兩個條件時,在部分 Android 手機(jī)上會出現(xiàn)文字顯示不全的問題。
fontWeight
譬如作者這臺手機(jī):
手機(jī)型號 | MIUI 版本 | Android 版本 |
---|---|---|
MI 8 | 12.5.2 | 10 |
就會出現(xiàn)文字顯示不全的問題,即使將 fontWeight
設(shè)置為 bold
,也不會有粗體效果。
但是只要 style 設(shè)置了 fontFamily
,就不會有顯示不全的問題,因此,怎樣才能正確地設(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 ) }
示例
這里有 一個示例 ,供你參考。
到此這篇關(guān)于React Native 中處理 Android 手機(jī)吞字的解決方案的文章就介紹到這了,更多相關(guān)React Native Android 手機(jī)吞字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解React setState數(shù)據(jù)更新機(jī)制
這篇文章主要介紹了React setState數(shù)據(jù)更新機(jī)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用React框架,感興趣的朋友可以了解下2021-04-04React源碼分析之useCallback與useMemo及useContext詳解
這篇文章主要介紹了React useCallback與useMemo及useContext源碼分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11React Native模塊之Permissions權(quán)限申請的實(shí)例相機(jī)
這篇文章主要介紹了React Native模塊之Permissions權(quán)限申請的實(shí)例相機(jī)的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)
這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié),需本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,要的朋友可以參考下2021-08-08React開發(fā)進(jìn)階redux saga使用原理詳解
這篇文章主要為大家介紹了React開發(fā)進(jìn)階redux saga使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11使用React?MUI庫實(shí)現(xiàn)用戶列表分頁功能
MUI是一款基于React的UI組件庫,可以方便地構(gòu)建美觀的用戶界面,使用MUI的DataTable組件和分頁器組件可以輕松實(shí)現(xiàn)用戶列表分頁功能,這篇文章使用MUI庫實(shí)現(xiàn)了用戶列表分頁功能,感興趣的同學(xué)可以參考下文2023-05-05jenkins分環(huán)境部署vue/react項(xiàng)目的方法步驟
這篇文章主要介紹了jenkins分環(huán)境部署vue/react項(xiàng)目的方法,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02React中的Props類型校驗(yàn)和默認(rèn)值詳解
這篇文章主要為大家詳細(xì)介紹了React中的Props類型校驗(yàn)和默認(rèn)值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03