gpt4 book ai didi

react-native - 如何在 react-native 中创建渐变文本

转载 作者:行者123 更新时间:2023-12-04 11:24:44 25 4
gpt4 key购买 nike

我想在 react-native 中创建具有线性渐变颜色的文本,但找不到合适的方法或包来执行此操作。我尝试使用这个包:https://github.com/iyegoroff/react-native-text-gradient .但是,在尝试使用 expo 运行示例时,它给了我以下错误:

TypeError: Cannot read property 'x' of undefined

This error is located at:
in RNLinearTextGradient (at App.js:26)
in RCTView (at View.js:60)
in View (at App.js:17)
in App (at registerRootComponent.js:35)
in RootErrorBoundary (at registerRootComponent.js:34)
in ExpoRootComponent (at renderApplication.js:33)
in RCTView (at View.js:60)
in View (at AppContainer.js:102)
in RCTView (at View.js:60)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:32)
at linear-text-gradient.js:16
at Object.render (create-text-gradient-class.js:219)
at finishClassComponent (ReactNativeRenderer-dev.js:8811)
at updateClassComponent (ReactNativeRenderer-dev.js:8761)
at beginWork (ReactNativeRenderer-dev.js:9580)
at performUnitOfWork (ReactNativeRenderer-dev.js:12924)
at workLoop (ReactNativeRenderer-dev.js:12953)
at renderRoot (ReactNativeRenderer-dev.js:12996)
at performWorkOnRoot (ReactNativeRenderer-dev.js:13632)
at performWork (ReactNativeRenderer-dev.js:13545)

你能帮我解决这个问题还是找到另一种在 react-native 中创建渐变文本的方法?

最佳答案

使用 [ react 原生 svg][1]

[1]:https://github.com/react-native-community/react-native-svg例如:

 import Svg, {
LinearGradient,
Text,
Defs,
Stop,
TSpan
} from 'react-native-svg';
<Svg viewBox="0 0 300 300" height="300"
width="300">
<Defs>
<LinearGradient id="rainbow" x1="0" x2="0" y1="0" y2="100%" gradientUnits="userSpaceOnUse" >
<Stop stopColor="#FF5B99" offset="0%" />
<Stop stopColor="#FF5447" offset="20%" />
<Stop stopColor="#FF7B21" offset="40%" />
<Stop stopColor="#EAFC37" offset="60%" />
<Stop stopColor="#4FCB6B" offset="80%" />
<Stop stopColor="#51F7FE" offset="100%" />
</LinearGradient>
</Defs>
<Text fill="url(#rainbow)">
<TSpan
fonSize="72"
x="0"
y="72"
>
gradient
</TSpan>
<TSpan fonSize="72" x="0" dy="72">text</TSpan>
<TSpan fonSize="72" x="0" dy="72">all up in here</TSpan>
</Text>
</Svg>

关于react-native - 如何在 react-native 中创建渐变文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51248255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com