[英]Is there a way to wrap background color around text in react-native?
[英]react-native: multi color text view
我想渲染一行文本,其中一些單詞以不同的顏色突出顯示。
理想情況下,我會使用帶有反應的 span 標簽來完成。
想知道我將如何對 react-native 做同樣的事情?
您可以通過使用嵌套的 Text 組件來實現這一點
<Text style={{color: 'blue'}}>
I am blue
<Text style={{color: 'red'}}>
i am red
</Text>
and i am blue again
</Text>
這是文檔的鏈接,可以更好地解釋它
你可以做得更好,我的方式:
CText = (props) => <Text style={{color: props.color}}>{props.children}</Text>
內部渲染添加:
const CText = this.CText
並返回
<Text>I am <CText color={'red'}>Blue color</CText> and <CText color={'blue'}>Blue color</CText></Text>
只是下一個<Text>
元素
<Text>
I am some text with
<Text style={{fontWeight: 'bold'}}>bold</Text>
stuff
<Text>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.