如何在React Native上创建一个角标?

huangapple go评论88阅读模式
英文:

How to create a corner ribbon on react native?

问题

我卡在创建卡片上的角标上。之前我使用react-native-svg库来完成这个任务,但是库存在问题。我如何在不使用任何库的情况下完成它?有没有办法通过CSS来做到这一点?

这个角标看起来像这样。

英文:

I'm stuck on creating a corner ribbon on a card. Before I used react-native-svg library to do this task. but having an issue with the library. How can I do it without using any library? is there a way to do it from CSS?

the ribbon is look like this.

如何在React Native上创建一个角标?

答案1

得分: 0

以下是您要翻译的代码部分:

const Banner = ({message, style}) => {
  return (
    <Text style={[styles.banner, style]}>
      {message}
    </Text>
  );
};

const styles = StyleSheet.create({
  banner: {
    position: 'absolute',
    right: -40,
    top: 20,
    width: 160,
    transform: [{ rotate: "45deg" }],
    backgroundColor: 'black',
    color: 'white',
    padding: 8,
    textAlign: 'center',
  },
});

const App = () => {
  return (
    <>
      <Image 
        source={{uri: 'https://picsum.photos/1080/1920'}} 
        resizeMode="cover"
        style={{width: '100%', height: '100%'}}
      />
      <Banner 
        message="PREMIUM" 
        style={{
          color: 'black', 
          backgroundColor: 'yellow', 
          fontWeight: 'bold',
        }}
      />
    </>
  )
}

详情请参考Transforms

英文:

Use absolute position for placement and transform to rotate. Something like this:

const Banner = ({message, style}) =&gt; {
  return (
    &lt;Text style={[styles.banner, style]}&gt;
      {message}
    &lt;/Text&gt;
  );
};

const styles = StyleSheet.create({
  banner: {
    position: &#39;absolute&#39;,
    right: -40,
    top: 20,
    width: 160,
    transform: [{ rotate: &quot;45deg&quot; }],
    backgroundColor: &#39;black&#39;,
    color: &#39;white&#39;,
    padding: 8,
    textAlign: &#39;center&#39;,
  },
});

const App = () =&gt; {
  return (
    &lt;&gt;
      &lt;Image 
        source={{uri: &#39;https://picsum.photos/1080/1920&#39;}} 
        resizeMode=&quot;cover&quot;
        style={{width: &#39;100%&#39;, height: &#39;100%&#39;}}
      /&gt;
      &lt;Banner 
        message=&quot;PREMIUM&quot; 
        style={{
          color: &#39;black&#39;, 
          backgroundColor: &#39;yellow&#39;, 
          fontWeight: &#39;bold&#39;,
        }}
      /&gt;
    &lt;/&gt;
  )
}

See Transforms for details.

huangapple
  • 本文由 发表于 2023年3月21日 01:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定