React native ReactNative Android:在嵌套文本中使用图像时如何将文本设置为图像
我使用嵌套的React native ReactNative Android:在嵌套文本中使用图像时如何将文本设置为图像,react-native,react-native-android,React Native,React Native Android,我使用嵌套的文本在文本之间添加图像。 之后,我尝试获取原始的字符串值,但是图像中的字符串被替换为I import * as React from 'react'; import { Text, View, StyleSheet,TextInput,Button,Image } from 'react-native'; import Constants from 'expo-constants'; // or any pure javascript modules available in np
文本
在文本之间添加图像
。
之后,我尝试获取原始的字符串
值,但是图像
中的字符串
被替换为I
import * as React from 'react';
import { Text, View, StyleSheet,TextInput,Button,Image } from 'react-native';
import Constants from 'expo-constants';
// or any pure javascript modules available in npm
export default class App extends React.Component {
state = {
text: '',
images:[],
}
changeText = (text) => {
this.setState({
text,
});
}
parseText = () => {
const { text,images } = this.state;
if (images.length !== 0){
return(
<Text>
{text}
{images}
</Text>
);
}
return text;
}
addImage = () => {
const { images } = this.state;
const newImage = (<Image
style={{width:20,height:20}}
source={require('./assets/snack-icon.png')} />);
this.setState({
images:[...images , newImage],
})
}
render() {
return (
<View style={styles.container}>
<TextInput
style={{flex:1}}
onChangeText={this.changeText}
placeholder="test"
>
{this.parseText()}
</TextInput>
<Button
title="add Image"
onPress={this.addImage}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
import*as React from'React';
从“react native”导入{Text,View,StyleSheet,TextInput,Button,Image};
从“expo常量”导入常量;
//或npm中可用的任何纯javascript模块
导出默认类App扩展React.Component{
状态={
文本:“”,
图像:[],
}
changeText=(文本)=>{
这是我的国家({
文本,
});
}
parseText=()=>{
const{text,images}=this.state;
如果(images.length!==0){
返回(
{text}
{图像}
);
}
返回文本;
}
addImage=()=>{
const{images}=this.state;
常量newImage=();
这是我的国家({
图像:[…图像,新图像],
})
}
render(){
返回(
{this.parseText()}
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
paddingTop:Constants.statusBarHeight,
背景颜色:“#ecf0f1”,
填充:8,
},
});
这个例子旨在说明大致情况,但与我的不一样
在本例中,当您添加图像并导入文本时,图像显示为I。
我能换成字符串而不是I吗?你能详细说明一下吗?您想将自定义图像设置为按钮还是在图像上显示文本。@AbdulBasit我想用Slack或FaceBook实现自定义表情。当将图像放入文本输入并发送到服务器时,它应该看起来像
Text-Text
,但当我编辑文本时,会更改为I。