繁体   English   中英

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM