简体   繁体   English

react-native:多色文本视图

[英]react-native: multi color text view

I want to render a single line of text with some words highlighted in a different color.我想渲染一行文本,其中一些单词以不同的颜色突出显示。

I would ideally do it with a span tag with react.理想情况下,我会使用带有反应的 span 标签来完成。

Wondering how would i do the same with react-native?想知道我将如何对 react-native 做同样的事情?

You can achieve this by using nested Text components您可以通过使用嵌套的 Text 组件来实现这一点

<Text style={{color: 'blue'}}>
    I am blue
    <Text style={{color: 'red'}}>
        i am red
    </Text>
    and i am blue again
</Text>

Here's a link to the documentation explaining it better这是文档的链接,可以更好地解释它

You can do it better, my way:你可以做得更好,我的方式:

CText = (props) => <Text style={{color: props.color}}>{props.children}</Text>

inside render add:内部渲染添加:

const CText = this.CText

and return并返回

<Text>I am <CText color={'red'}>Blue color</CText> and <CText color={'blue'}>Blue color</CText></Text>

Simply next <Text> elements只是下一个<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