简体   繁体   中英

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.

Wondering how would i do the same with react-native?

You can achieve this by using nested Text components

<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>
    I am some text with 
    <Text style={{fontWeight: 'bold'}}>bold</Text> 
    stuff
<Text>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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