|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + ScrollView, |
| 4 | + Text, |
| 5 | + View, |
| 6 | + Dimensions, |
| 7 | + TextInput |
| 8 | +} from 'react-native'; |
| 9 | + |
| 10 | +import RTNTextSize, { TSFontSpecs, TSFontInfo } from 'react-native-text-size' |
| 11 | + |
| 12 | +export function TextSizeExample() { |
| 13 | + const [res, setRes] = useState<string[]>([]) |
| 14 | + const [res2, setRes2] = useState<string[]>([]) |
| 15 | + const fontFamily = 'HarmonyOS Sans SC' |
| 16 | + const [texts, setTexts] = useState<string>('I rnTextSize ❤️') |
| 17 | + const [texts3, setTexts3] = useState<string>('') |
| 18 | + const [width, setWidth] = useState<number>(); |
| 19 | + const [height, setHeight] = useState<number>(); |
| 20 | + const fontSpecs: TSFontSpecs = { |
| 21 | + fontFamily: undefined, |
| 22 | + fontSize: 20, |
| 23 | + fontStyle: 'normal', //正斜 |
| 24 | + fontWeight: '700', |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + type State = { heights2: number[] } |
| 29 | + const [texts2, setTexts2] = useState(['I ❤️ rnTextSize', 'I ❤️ rnTextSize using flatHeights', 'Thx for flatHeights', 'test123',]) |
| 30 | + const [heights2, setHeights2] = useState<number[]>([]); |
| 31 | + const fontSpecs2: TSFontSpecs = { |
| 32 | + fontFamily: undefined, |
| 33 | + fontSize: 20, |
| 34 | + fontStyle: 'italic', |
| 35 | + fontWeight: 'bold', |
| 36 | + } |
| 37 | + |
| 38 | + const [key, setKey] = useState({}) |
| 39 | + |
| 40 | + |
| 41 | + const [info, setInfo] = useState<TSFontInfo>() |
| 42 | + const fontSpecs3: TSFontSpecs = { |
| 43 | + fontFamily: 'harmony', |
| 44 | + textBreakStrategy: 'balanced', |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + const myFun = async () => { |
| 50 | + console.log(" fontFamilyNames++++++++++++++"); |
| 51 | + const resp = await RTNTextSize.fontFamilyNames(); |
| 52 | + setRes(resp) |
| 53 | + |
| 54 | + console.log(" fontFamilyNames++++++++++++++"); |
| 55 | + const resp2 = await RTNTextSize.fontNamesForFamilyName(fontFamily); |
| 56 | + setRes2(resp2) |
| 57 | + |
| 58 | + console.log(" measure++++++++++++++"); |
| 59 | + setTexts(texts) |
| 60 | + const width = Dimensions.get('window').width * 0.8 |
| 61 | + const newHeights = await RTNTextSize.measure({ |
| 62 | + text: texts, // array of texts to measure, can include symbols |
| 63 | + width: width, // max-width of the "virtual" container |
| 64 | + ...fontSpecs, // RN font specification |
| 65 | + }) |
| 66 | + setHeight(newHeights.height); |
| 67 | + setWidth(newHeights.width) |
| 68 | + |
| 69 | + console.log(" flatHeights++++++++++++++"); |
| 70 | + setTexts2(texts2) |
| 71 | + const width2 = Dimensions.get('window').width * 0.8 |
| 72 | + const newHeights2 = await RTNTextSize.flatHeights({ |
| 73 | + text: texts2, // array of texts to measure, can include symbols |
| 74 | + width: width2, // max-width of the "virtual" container |
| 75 | + ...fontSpecs2, // RN font specification |
| 76 | + }) |
| 77 | + setHeights2(newHeights2); |
| 78 | + |
| 79 | + const keyp = await RTNTextSize.specsForTextStyles(); |
| 80 | + setKey(keyp) |
| 81 | + |
| 82 | + const infos = await RTNTextSize.fontFromSpecs(fontSpecs3); |
| 83 | + setInfo(infos) |
| 84 | + |
| 85 | + |
| 86 | + } |
| 87 | + myFun() |
| 88 | + }, []) |
| 89 | + |
| 90 | + return ( |
| 91 | + <ScrollView style={{ flexGrow: 1 }}> |
| 92 | + <View style={{ paddingLeft: 12, paddingRight: 12 }}> |
| 93 | + |
| 94 | + <Text> |
| 95 | + ----------------measure:[fontFamily: undefined,fontSize: 20,fontStyle: 'normal', 正斜fontWeight: '700'] |
| 96 | + </Text> |
| 97 | + <Text style={{ |
| 98 | + width: width, |
| 99 | + height: height, |
| 100 | + ...fontSpecs |
| 101 | + }}> |
| 102 | + {texts} |
| 103 | + </Text> |
| 104 | + |
| 105 | + |
| 106 | + <TextInput |
| 107 | + value={texts} |
| 108 | + onChangeText={value => { setTexts(value) }} |
| 109 | + style={{ width: '100%', height: 28, borderWidth: 2, borderColor: 'black' }} |
| 110 | + /> |
| 111 | + |
| 112 | + <Text> |
| 113 | + ---------------- |
| 114 | + flatHeights:[fontFamily: undefined,fontSize: 20,fontStyle: 'italic', fontWeight: 'bold'] |
| 115 | + </Text> |
| 116 | + {texts2.map( |
| 117 | + (texts2, index) => ( |
| 118 | + <Text style={{ height: heights2[index], ...fontSpecs2 }}> |
| 119 | + {texts2} |
| 120 | + </Text> |
| 121 | + ) |
| 122 | + )} |
| 123 | + <TextInput |
| 124 | + value = {texts3} |
| 125 | + onChangeText={value => { |
| 126 | + let value2 =value.split(","); |
| 127 | + setTexts3(value) |
| 128 | + setTexts2(value2) |
| 129 | + }} |
| 130 | + style={{ width: '100%', height: 28, borderWidth: 2, borderColor: 'black' }} |
| 131 | + /> |
| 132 | + |
| 133 | + |
| 134 | + <Text> |
| 135 | + ----------------specsForTextStyles:获取系统默认配置的字体的具体信息 |
| 136 | + </Text> |
| 137 | + <Text> |
| 138 | + {JSON.stringify(key)} |
| 139 | + </Text> |
| 140 | + |
| 141 | + <Text> |
| 142 | + ----------------fontFamilyNames:获取系统默认配置的字体 |
| 143 | + </Text> |
| 144 | + <Text> |
| 145 | + {res} |
| 146 | + </Text> |
| 147 | + |
| 148 | + |
| 149 | + <Text> |
| 150 | + ----------------fontNamesForFamilyName:获取系统字体的属性 |
| 151 | + </Text> |
| 152 | + <Text> |
| 153 | + {res2} |
| 154 | + </Text> |
| 155 | + |
| 156 | + |
| 157 | + <Text> |
| 158 | + ----------------fontFromSpecs:返回从给定规范中获得的字体特征 |
| 159 | + </Text> |
| 160 | + <Text> |
| 161 | + {JSON.stringify(info)} |
| 162 | + </Text> |
| 163 | + |
| 164 | + |
| 165 | + </View> |
| 166 | + </ScrollView> |
| 167 | + ) |
| 168 | +} |
0 commit comments