代码之家  ›  专栏  ›  技术社区  ›  Lau Kumra

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

  •  2
  • Lau Kumra  · 技术社区  · 6 年前

    我想在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中创建渐变文本?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Sidali Hallak    6 年前

    使用[React Native 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>
    
        2
  •  -1
  •   Florin Dobre    6 年前

    有两个包可用于渐变文本:

    (一) react-native-text-gradient

    <LinearTextGradient
      style={{ fontWeight: 'bold', fontSize: 72 }}
      locations={[0, 1]}
      colors={['red', 'blue']}
      start={{ x: 0, y: 0 }}
      end={{ x: 1, y: 0 }}
    >
      THIS IS TEXT GRADIENT
    </LinearTextGradient>
    

    结果如下:

    enter image description here

    资料来源: https://github.com/iyegoroff/react-native-text-gradient

    2个) react-native-linear-gradient 以下内容:

    import LinearGradient from 'react-native-linear-gradient';
    
    const styles = StyleSheet.create({
      text: {
        color: 'black',
        fontSize: 14,
      },
      gradient: {
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
      },
    });
    
       <View>
        <View>
          <Text style={styles.text}>
             This is a gradiented text
          </Text>
          <Text style={styles.text}>
             This is a gradiented text
          </Text><Text style={styles.text}>
             This is a gradiented text
          </Text><Text style={styles.text}>
             This is a gradiented text
          </Text><Text style={styles.text}>
             This is a gradiented text
          </Text>
        </View>
         <LinearGradient
            start={{ x: 0.0, y: 0.0 }}
            end={{ x: 0.0, y: 1.0 }}
            locations={[0.0, 1.0]}
            colors={['#ffffff40', '#fffffff5']} //<-- last 2 chars from color control the opacity
            useViewFrame={false}
            style={styles.gradient}
          />
        <View>
    

    结果如下:

    enter image description here

    更多的反应本机线性梯度信息如下:

    https://github.com/react-native-community/react-native-linear-gradient