如何在Shopware 6.5.1中添加自定义字体?

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

How to add custom fonts in Shopware 6.5.1?

问题

有人成功将自定义字体添加到Shopware 6.5.1商店前端吗?

我尝试过但都失败了:

  1. customtheme/src/Resources/app/storefront/src/assets/font/font.woff2 中添加字体文件。
  2. $app-css-relative-asset-path 变量指向这个目录,并在 base.scss 中添加以下内容:
@font-face {
    font-family: 'Myriad Pro';
    font-style: normal;
    font-weight: 300;
    font-display: fallback;
    src: url('#{$app-css-relative-asset-path}/font/font.woff2') format('woff2'),
    url('#{$app-css-relative-asset-path}/font/font.woff') format('woff');
}

这根本不起作用。甚至不会更改商店前端中字体的路径。base.scss 中的其他scss更改是有效的。

在Shopware 6.4中,这种方法是有效的。

英文:

Did anybody successfully add custom fonts to Shopware 6.5.1 storefront?

What I tried to do but all failed:

  1. add in customtheme/src/Resources/app/storefront/src/assets/font/font.woff2
  2. point $app-css-relative-asset-path variable to this directory and add the following to base.scss
@font-face {
    font-family: 'Myriad Pro';
    font-style: normal;
    font-weight: 300;
    font-display: fallback;
    src: url('#{$app-css-relative-asset-path}/font/font.woff2') format('woff2'),
    url('#{$app-css-relative-asset-path}/font/font.woff') format('woff');
}

This does not work at all. It does not even change the path to the font in storefront. Other scss changes in base.scss are working.

In shopware 6.4 this aproach worked, though.

答案1

得分: 2

这是对我来说在6.4.x到6.5.1.1版本上的工作方式:

  • 将您的资源放在custom/apps/MyTheme/Resources/public/assets/fonts目录下。
  • 在overrides.scss中定义您的字体及其相对路径,如下所示:
$font-family-base: 'my-font', Helvetica, Arial, sans-serif;
$headings-font-family: 'my-font', Helvetica, Arial, sans-serif;

$my-asset-path: '#{$sw-asset-theme-url}/bundles/mytheme/assets' !default;
  • 在base.scss中定义字体样式,如下所示:
@font-face {
  font-family: 'my-font';
  src: url('#{$my-asset-path}/fonts/my-font.eot?#iefix') format('eot'),
  url('#{$my-asset-path}/fonts/my-font.woff2') format('woff2'),
  url('#{$my-asset-path}/fonts/my-font.woff') format('woff'),
  url('#{$my-asset-path}/fonts/my-font.ttf') format('truetype');
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}
英文:

This is the way it works for me with 6.4.x to 6.5.1.1

  • put your assets in custom/apps/MyTheme/Resources/public/assets/fonts
  • define your fonts and the relative path in overrides.scss like
$font-family-base: 'my-font', Helvetica, Arial, sans-serif;
$headings-font-family: 'my-font', Helvetica, Arial, sans-serif;

$my-asset-path: '#{$sw-asset-theme-url}/bundles/mytheme/assets' !default;
  • Define the font-faces in base.scss like
@font-face {
  font-family: 'my-font';
  src: url('#{$my-asset-path}/fonts/my-font.eot?#iefix') format('eot'),
  url('#{$my-asset-path}/fonts/my-font.woff2') format('woff2'),
  url('#{$my-asset-path}/fonts/my-font.woff') format('woff'),
  url('#{$my-asset-path}/fonts/my-font.ttf') format('truetype');
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

答案2

得分: 0

你使用 $app-css-relative-asset-path 的方法并没有错。你是否使用了相对路径?

在 Shopware 6.5 中,在 custom/plugins/CUSTOMTHEME/src/Resources/app/storefront/src/assets/ 创建一个名为 "fonts" 的文件夹。

在使用任何 @font-face 之前,你需要设置两个变量。最好使用一个单独的文件来存放字体。

将以下代码放在文件的顶部或者在你的第一个 @font-face 之前:

_fonts.scss

/// @deprecated tag:v6.5.0 Use `app-css-relative-asset-path` instead
$asset-path: '#{$sw-asset-theme-url}/bundles/storefront/assets' !default;
$app-css-relative-asset-path: '../assets' !default;

然后在你的 @font-face 中,继续你已经做过的操作:

@font-face {
  font-family: 'my-font';
  src: url('#{$app-css-relative-asset-path}/fonts/my-font.ttf') format('truetype');
}

附注

将字体放在 public 文件夹中会在运行 build-administration 脚本后移除它们,所以我不建议这样做。

英文:

Your approach with $app-css-relative-asset-path is not wrong. Did you use a relative path?

In Shopware 6.5 create a fonts folder in custom/plugins/CUSTOMTHEME/src/Resources/app/storefront/src/assets/.

Before you use any @font-face you need to set two variables. A separate file for fonts is preferred.

Put the following code at the top of the file or above your first @font-face.

_fonts.scss

/// @deprecated tag:v6.5.0 Use `app-css-relative-asset-path` instead
$asset-path: '#{$sw-asset-theme-url}/bundles/storefront/assets' !default;
$app-css-relative-asset-path: '../assets' !default;

And then for your @font-face, do what you've already done.

@font-face {
  font-family: 'my-font';
  src: url('#{$app-css-relative-asset-path}/fonts/my-font.ttf') format('truetype');
}

Side note

Putting your fonts in the public folder will remove them as soon as you run the build-administration script. So I wouldn't recommend that.

huangapple
  • 本文由 发表于 2023年6月1日 17:01:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380272.html
匿名

发表评论

匿名网友

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

确定