简体   繁体   English

如何将本地自定义字体应用于 Stripe Elements?

[英]How do I apply a local custom font to Stripe Elements?

I am trying to use a font from a local .otf file inside my project's directory in order to style the text within the inputs of the credit card form provided by Stripe Elements.我正在尝试使用项目目录中本地.otf文件中的字体,以便在 Stripe Elements 提供的信用卡表单输入中设置文本样式。

As this is a React project, I have been making use of the react-stripe-elements library.由于这是一个 React 项目,我一直在使用react-stripe-elements库。 I have been referencing the following docs in tandem:我一直在参考以下文档:


Here is where I attempt to use my desired font by passing it in through the fonts prop as specified in the react-stripe-elements docs:这是我尝试通过react-stripe-elements文档中指定的fonts prop 传递我想要的字体来使用它的地方:

<Elements
  fonts={[
    {
      family: 'geog',
      src: 'url(../../../shared/styles/fonts/Geogtq-Rg.otf)',
      style: 'normal',
      weight: '400',
    },
  ]}
>
  <StripeCCForm />
</Elements>

With this code, I get the following error:使用此代码,我收到以下错误:

Invalid src value in font configuration: ../../../shared/styles/fonts/Geogtq-Rg.otf. URLs have to start with 'https://' or 'data:'


I was under the impression that the src value could be a relative path to a local font, so I am quite confused as to why I am getting this error.我的印象是src值可能是本地字体的相对路径,所以我很困惑为什么我会收到这个错误。 Any insight would be greatly appreciated!任何见解将不胜感激!

I'm working with Vue, and I originally had an answer here about having success with a require statement for src , but it turns out I didn't have to pass anything to stripe.elements at all.我正在使用 Vue,我最初在这里有一个关于使用srcrequire语句获得成功的答案,但事实证明我根本不需要向stripe.elements传递任何内容。 Instead, I have this:相反,我有这个:

const stripeStyle = {
  style: {
    base: {
      fontFamily: 'Font-Name',
      // fontSize, etc. if applicable
    },
  },
};

const elements = stripe.elements();
this.cardNumber = elements.create('cardNumber', stripeStyle);
// etc.

Here's the important part: The value for fontFamily matches the filename I import through a Sass file.这是重要的部分: fontFamily的值我通过 Sass 文件导入文件名匹配 For this example, I would have:对于这个例子,我会有:

@font-face {
  font-family: "My Font";
  src: url("~@/assets/fonts/Font-Name.otf") format ("OpenType");
}

I'm not sure why this works, but hopefully it's possible for you to go about it this way.我不确定为什么会这样,但希望您可以这样做。

This issue has been addressed here: https://github.com/stripe/react-stripe-elements/issues/285此问题已在此处解决: https : //github.com/stripe/react-stripe-elements/issues/285

I was just searching for the same and it worked.我只是在寻找相同的东西并且它起作用了。 It didn't work on local because of not having https, but works perfectly in the production.由于没有 https,它在本地无法运行,但在生产中运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM