Home > Mobile >  React Native - multiple colors inside the same Text component without nesting?
React Native - multiple colors inside the same Text component without nesting?

Time:10-22

Each Text component can have it's own color, I'm aware of that. The problem that I'm facing is I'm working with Arabic and trying to change the color of each letter.

The letters tend to change form when beside certain other letters (unlike English). So for example in English "a" will always look like "a" regardless of whether it's beside a "b" or a "c". Not so with Arabic. The letters visually change form depending on what's around them.

So for that reason I can't have many different Text components with their own style because they will be forced to be separate and thus visually inaccurate. They need to be inside one Text component. But how can I make it have different colors for each letter?

Is this possible? Any tricks?

CodePudding user response:

I'm not sure if it works in your case since you didn't provide any code, but you should be able to group the separated Text components inside another Text component.

Example: https://snack.expo.dev/@truetiem/arabic-separated-chars

<Text style={{fontSize: 42}}>
  <Text style={{color: "red"}}>أ</Text>
  <Text style={{color: "blue"}}>ه</Text>
  <Text style={{color: "purple"}}>ل</Text>
  <Text style={{color: "green"}}>اً</Text>
  <Text> </Text>
  <Text style={{color: "gold"}}>ب</Text>
  <Text style={{color: "aqua"}}>ك</Text>
</Text>
  • Related