react-native - 我想为 React-Native TextInput 的每个字符值使用不同的颜色和字体系列
问题描述
我的代码:
export default class ClearComponent extends Component {
color= ['red', 'blue']
font_family= ['normal', 'notoserif', 'sans-serif']
state = {
text: '',
context: [
],
color:"black",
font_family:"normal",
};
textInputOnChange = (text) => {
const {context, color, font_family} = this.state;
if (context.length > text.length) {
context.pop()
this.setState({
context
})
} else {
this.setState({
context:[...context,{
value: text[text.length - 1],
color,
font_family
}]
})
}
};
render() {
const {context} = this.state;
const { color, font_family} = this
return (
<View>
<TextInput onChangeText={this.textInputOnChange}>
{context.map((element) => {
return (
<Text
style={{fontFamily: element.font_family, color: element.color,fontSize:25}}>
{element.value}
</Text>
);
})}
</TextInput>
<Picker
selectedValue={this.state.font_family}
mode="dropdown"
onValueChange={(itemValue, itemIndex) => {
this.setState({font_family: itemValue});
}}
>
{font_family.map((element) => {
return (
<Picker.Item
key={(key) => key.id}
value={element}
label={element}
/>
);
})}
</Picker>
<View style={{flexDirection: 'row'}}>
{color.map((element) => {
return (
<TouchableOpacity onPress={()=>{
this.setState({color:element})
console.log(this)}}>
<Text
style={{
height: 25,
width: 25,
backgroundColor: element,
borderRadius: 20,
marginHorizontal: 2,
marginVertical: 2,
}}/>
</TouchableOpacity>
);
})}
</View>
<Text style={{fontFamily:"",fontSize:40}}>dsadas</Text>
</View>
);
}
}
我正在开发一个笔记本应用程序。我也遇到过这样的问题,我找到了一些解决方案,但还不够,它带来了很多问题。
我想为每个字符制作不同的样式定义,但这段代码效果不佳。
有专门的图书馆吗?
我对您的替代解决方案持开放态度。
解决方案
推荐阅读
- android - 从另一个片段导航回来时,片段内的 ViewPager 为空
- google-api - 谷歌云文字转语音:服务器响应状态为 403()
- tensorflow - 张量流中 tf.contrib.rnn.BasicLSTMCell 中构建函数的输入是什么
- java - 这个代码怎么会出现一个数字三角形?
- windows - 如何在不删除或操作任何内容的情况下将二进制内容加载到剪贴板?(自动热键)
- java - 如何使用保存的首选项在文本视图中添加点并在 24 小时后将这些点转移到另一个文本视图?
- php - Laravel AJAX 循环
- yocto - 从 ext4、dtb 和 uboot.bin 制作 img
- android - 如何优雅地关闭适用于 Android 的 React Native 应用程序
- laravel - 发送邮件检查它是否已交付并在 Laravel 中向收件人开放