I'm looking for practical direction in creating a <GradientText />
component in React Native. I have this created for the web side of things, using -webkit-background-clip
and CSS linear gradients for Webkit browsers and defaulting to SVG masks for non-webkit browsers (which only supports two color stops, the webkit version can obviously support more).
I'd like to learn how to do this with React Native. Here is my done critera:
- Support two-stop color gradients (more than two, if I can get it).
- Gradient direction can be arbitrary angles or a string like "to top", "to bottom", "to left" or "to right".
- Color opacity should be supported, so ideally the component can take a HEX color or RGBA color.
The resulting API should look something like this:
<GradientText colors={['#313182', 'rgba(244,33,233,.5)']} direction='<to top> | <185deg>'>Hello!</GradientText>
This is where it gets tricky for me. I know enough about iOS development to be dangerous and nothing about Android development, but for iOS, it seems like the following could work, but I'm not sure about best practices:
- Subclass the
<Text>
component of React Native. - Create a
UIImage
for the gradient image. - Use that image as a pattern fill for the rendered text.
Something like this.
Then I'd like to do the same thing on Android using a Shader
. Again, I'm looking for best practices on how to go about that.
Finally, my hope is that I can basically inherit all of the <Text>
component's capabilities, and just pepper in my own additions for affecting the color. I'm curious to know how realistic this would be and maybe some pitfalls I haven't considered (or probably don't know about).
I'm not looking for someone to do the work for me, but I am looking for some practical tips, some ideas on where to look for information and how to structure the project correctly, so that in the end, I can share it back to the community. Thanks!