React Native为<Text>字段中的单个单词添加粗体或斜体

如何在文本字段中使单个单词变为粗体或斜体? 有点像这样:

<Text>This is a sentence <b>with</b> one word in bold</Text> 

如果我为粗体字创build了一个新的文本字段,它会将它分隔到另一行,所以肯定不是这样做的。 这就像在<p>标签内创build一个<p>标签,只是为了让一个字粗体。

对于其他文本组件,您可以像使用容器一样使用<Text> 。 这是例子:

 ... <Text> <Text>This is a sentence</Text> <Text style={{fontWeight: "bold"}}> with</Text> <Text> one word in bold</Text> </Text> ... 

这是一个例子 。

对于更像networking的感觉:

const B = (props) => <Text style={{fontWeight: 'bold'}}>{props.children}</Text>

<Text>I'm in <B>bold</B> yo.</Text>

使用这个反应本地库

安装

npm install react-native-htmlview --save

基本用法

  import React from 'react'; import HTMLView from 'react-native-htmlview'; class App extends React.Component { render() { const htmlContent = 'This is a sentence <b>with</b> one word in bold'; return ( <HTMLView value={htmlContent} /> ); } } 

支持几乎所有的html标签。

用于更高级的用法

  1. 链接处理
  2. 自定义元素呈现

查看这个自述