如何在React Native中使文本变为粗体,斜体或下划线?

Jam*_* Ko 18 react-native react-native-text react-native-android react-native-ios

令人惊讶的是,在Stack Overflow上没有一个问题可以将这些组合在一起.关于斜体或强调的SO没有答案,事实上,只有这个问题是大胆的.我在下面自我回答了这个问题.

Jam*_* Ko 28

<Text style={styles.bold}>I'm bold!</Text>
<Text style={styles.italic}>I'm italic!</Text>
<Text style={styles.underline}>I'm underlined!</Text>

const styles = StyleSheet.create({
    bold: {fontWeight: 'bold'},
    italic: {fontStyle: 'italic'},
    underline: {textDecorationLine: 'underline'}
})
Run Code Online (Sandbox Code Playgroud)

关于Snack的工作演示:https://snack.expo.io/BJT2ss_y7


小智 23

仅使用

<Text style={styles.textStyle}>I'm Underline!</Text>

const styles = StyleSheet.create({
  textStyle: {
    textDecorationLine: 'underline'
  }
})
Run Code Online (Sandbox Code Playgroud)

其他装饰品有:

  1. 没有任何
  2. 强调
  3. 直通
  4. 下划线直通


Asa*_*hry 8

只有一条线解决方案

<Text style={{fontStyle: 'italic', fontWeight: 'bold', textDecorationLine: 'underline'}}>Bold, Italic & Underline Text</Text>
Run Code Online (Sandbox Code Playgroud)


Meh*_*nak 7

你可以在这个页面看到所有可能的 att https://reactnative.dev/docs/text

例如 ...

textDecorationLine: enum('none', 'underline', 'line-through', 'underline line-through')
Run Code Online (Sandbox Code Playgroud)


Shi*_*m 0 6

<View style={styles.MainContainer}>
    <Text style={styles.TextStyle}>Example of Underline Text</Text>
</View>
Run Code Online (Sandbox Code Playgroud)
<View style={styles.MainContainer}>
    <Text style={styles.TextStyle}>Example of Underline Text</Text>
</View>
Run Code Online (Sandbox Code Playgroud)

  • 多谢。“fontstyle”中有一个拼写错误。正确的语法是`fontStyle`,带有大写的“S”。 (3认同)