I would like to highlight a multiline text in a React Native app by changing the text's background color. The problem is that the background color changes in the whole text area, not just under the words.
class Example extends Component {
render() {
const text = '...';
return (
<View style={styles.textContainer}>
<Text style={styles.textStyle}>
{text}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
textContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
width: 200,
},
textStyle: {
backgroundColor: 'red',
},
});
The code above results in something that looks like this:
But I would like it to look like this:
I can get that result by splitting the text and adding the background color to the individual words:
class Example extends Component {
render() {
const text = '...';
const brokenText = text.split('').map(word => (
<Text style={styles.textStyle}>{word} </Text>
));
return (
<View style={styles.textContainer}>
{brokenText}
</View>
);
}
}
But splitting the text into individual words doesn't seem like the best solution, and has a huge performance cost. Is there any cleaner way to do it?