Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 506
Chirag Jain Avatar asked Mar 31 '15 15:03

Chirag Jain


3 Answers

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

like image 107
mttcrsp Avatar answered Oct 29 '22 04:10

mttcrsp


Simply nest <Text> elements

<Text>
    I am some text with 
    <Text style={{fontWeight: 'bold'}}>bold</Text> 
    stuff
<Text>
like image 3
sga4 Avatar answered Oct 29 '22 06:10

sga4


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>
like image 2
Idan Avatar answered Oct 29 '22 05:10

Idan