我在我的代码中使用 react-native-awesome-alerts 。在警报消息中,我想将文本分成新行
它应该像下图
请帮我怎么做
这是我的代码
import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import AwesomeAlert from 'react-native-awesome-alerts';
export default class Alert extends Component {
constructor(props) {
super(props);
this.state = { showAlert: false };
}
showAlert = () => {
this.setState({
showAlert: true
});
};
hideAlert = () => {
this.setState({
showAlert: false
});
};
render() {
const { showAlert } = this.state;
return (
<View style={styles.container}>
<Text>I'm AwesomeAlert</Text>
<TouchableOpacity onPress={() => {
this.showAlert();
}}>
<View style={styles.button}>
<Text style={styles.text}>Try me!</Text>
</View>
</TouchableOpacity>
<AwesomeAlert
show={showAlert}
showProgress={false}
message='Incorrect Username/Password Used{“\n”}Please try again…'
messageStyle={styles.textStyle}
closeOnTouchOutside={true}
closeOnHardwareBackPress={false}
contentContainerStyle={styles.alertStyle}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
},
button: {
margin: 10,
paddingHorizontal: 10,
paddingVertical: 7,
borderRadius: 5,
backgroundColor: '#AEDEF4',
},
text: {
color: '#fff',
fontSize: 15
},
alertStyle: {
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
height: 100,
width: '60%',
borderWidth: 1,
borderColor: '#fff',
borderRadius: 7,
color: 'red'
},
textStyle: {
fontSize: 14,
color: 'black',
alignItems: 'center'
}
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试过“使用的用户名/密码不正确{“\n”}请再试一次……”但仍然没有运气
请让我知道哪里出错了
您需要将消息字符串更改为使用反引号 ` 并添加 \n。这现在应该可以工作了。
message={`Incorrect Username/Password Used \n Please try again…`}
Run Code Online (Sandbox Code Playgroud)
您还可以将容器宽度更改为 80% 并添加textAlign:"centre"到 textStyle CSS 以使其看起来更好。
这是我设法生产的:
| 归档时间: |
|
| 查看次数: |
7363 次 |
| 最近记录: |