how to set background color to the text only in react native
Asked Answered
P

6

10

in html I can achieve this by

<span style="background-color:red">ketan</span>

but in react native how to implement this so that color will be applied to the text only.

Pedo answered 16/7, 2018 at 9:5 Comment(1)
facebook.github.io/react-native/docs/style.html Look for styling tips in docs.Octopus
Z
24

If you want the background color to only encompass the text, you can use this:

<Text style={{backgroundColor: 'blue', alignSelf: 'flex-start'}}>
    Ketan
</Text>

And just change alignSelf as needed, you need to set this property if you dont want the text to take the whole width of the container

Zima answered 9/5, 2019 at 3:25 Comment(2)
Worked like a charm. Not having AlignSelf was the issueMelodie
Worked for me, also you can add a parent view that you can set properties like: alignSelf: 'flex-start', borderRadius: 10, overflow: 'hidden', backgroundColor: 'red',Spadiceous
N
3

In react native you can directly use "Text" component and apply "backgroundColor" style to it as follows:

<Text> 
    <Text style={{ backgroundColor: "red", color: "white" }}>color
    </Text>
</Text>
Nebo answered 16/7, 2018 at 9:39 Comment(7)
<Text style={{backgroundColor: 'red'}}>ketan </Text>Pedo
@ketan kulkarni, not getting what you mean to say here.Nebo
@ Sandip Lipane I have tried many combinations like applied background colour to text, view but still, it is taking the full width I am trying to design a button using TouchableOpacity and Text and i want background color applied to the text onlyPedo
@ketan kulkarni, Please visit this link facebook.github.io/react-native/docs/text.html and try modifying existing examples with backgroundColor style. I have verified and it is working. Can you please verify it once?Nebo
it is working if i write <Text> background <Text style={{ backgroundColor: "red", color: "white" }}>color</Text> </Text>Pedo
Yes, you have to add nested text.Nebo
this works for me <Text> <Text style={{ backgroundColor: "red", color: "white" }}>color</Text> </Text>Pedo
F
1

In react native you have Text component to display text.

To set color of Text, you can use color property of it

<Text style={{color:'#ff0000'}}>Your Text</Text>

And for background color of that text, you can use View and than set backgroundColor of that View

Firecracker answered 16/7, 2018 at 9:9 Comment(1)
i have tried following 1) <View style={{ backgroundColor: "red" }} > <Text>n</Text> </View> 2) <View> <Text style={{ backgroundColor: "red" }} > n </Text> </View> but output is samePedo
C
0

you can't on IOS. set backgroundColor for extra View

<View style={{backgroundColor: 'red',flex:1}}><Text >ketan
    </Text></View>
Capricorn answered 16/7, 2018 at 10:23 Comment(2)
try with flex:1 or flex:0Capricorn
this works for me <Text> <Text style={{ backgroundColor: "red", color: "white" }}>color</Text> </Text> Pedo
G
0

try this one:

<Text style={{backgroundColor:'red', color:'white'}}>{'Message'}</Text>
Glockenspiel answered 16/7, 2018 at 13:10 Comment(0)
T
0
import { Text } from 'react-native';

<Text style={{backgroundColor: 'green'}}>
  Ketan
</Text>

In order to put text in ReactNative, you need Text component. And you can apply backgroundColor onto style property.

Here's a snack as a demo to show usage and as playground for you to play around

Tice answered 17/7, 2018 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.