구글 AMP – Font

4 min read

Amp 글꼴은 기본적으로 amp 페이지에 대한 사용자 지정 글꼴을 트리거하고 모니터링하는 데 도움이 되는 amp의 amp 구성 요소입니다. 이 글에서는 amp-font에 대해 자세히 설명합니다.

Amp-font를 사용하려면 다음 자바스크립트 파일을 추가해야 합니다.

<script async custom-element="amp-font" src="https://cdn.ampproject.org/v0/amp-font-0.1.js"></script>

Amp-font 구성 요소는 글꼴 로드에 걸리는 시간을 제어하는데 사용됩니다. ms 초 단위의 시간이 걸리는 timeout 속성이 있으며 기본적으로 3000ms입니다. 구성 요소는 필요한 글꼴이 로드되었는지 또는 오류 상태가 되었는지에 따라 document.documentElement 또는 document.body에서 클래스를 추가 / 제거할 수 있습니다.

amp-font 태그의 형식은 다음과 같습니다.

<amp-font layout="nodisplay" 
          font-family="Roboto Italic"
          timeout="2000"
          on-error-remove-class="robotoitalic-loading" 
          on-error-add-class="robotoitalic-missing"
          on-load-remove-class="robotoitalic-loading"
          on-load-add-class="robotoitalic-loaded">
</amp-font>

amp 페이지에서 amp-font를 사용하는 방법에 대한 작업 예는 다음과 같습니다.

예제

<!doctype html>
<html ⚡ lang="ko">
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <title>Google AMP - Amp Font</title>
  <link rel="canonical" href="https://googleblogamp.blogspot.com/2021/01/google-amp-font.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async custom-element="amp-font" src="https://cdn.ampproject.org/v0/amp-font-0.1.js"></script>
  <style amp-custom>
    @font-face {
      font-family: 'This font is not available';
      font-style: normal;
      font-weight: 300;
      src: url(fonts/MissingFont.ttf) format('truetype');
    }
    .font-missing {
      color:red;
      font-size:25px;
    }
  </style>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
</head>
<body>
<h1>Google AMP - Amp Font</h1>
<amp-font layout="nodisplay"
          font-family="Font Does Not exist"
          timeout="2000"
          on-error-remove-class="font-missing"
          on-error-add-class="font-error"
          on-load-remove-class="font-missing"
          on-load-add-class="font-loaded">
</amp-font>
<p class="font-missing">
  로드할 글꼴 파일이 작동할 때 on-error-remove-class, on-error-add-class, on-load-remove-class 및 on-load-add-class 속성이 작동하는 방식을 보여주는 amp 글꼴 구성 요소의 예는 존재하지 않습니다.
</p>
</body>
</html>

결과

위에 제공된 샘플 코드의 출력은 다음과 같습니다.

글꼴 파일이 성공적으로 로드될 때 amp-font의 예는 다음과 같습니다.

예제

<!doctype html>
<html ⚡ lang="ko">
<head>
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <title>Google AMP - Amp Font</title>
  <link rel="canonical" href="https://googleblogamp.blogspot.com/2021/01/google-amp-font.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async custom-element="amp-font" src="https://cdn.ampproject.org/v0/amp-font-0.1.js"></script>
  <style amp-custom>
    @font-face {
      font-family: 'This font is not available';
      font-style: normal;
      font-weight: 300;
      src: url(fonts/MissingFont.ttf) format('truetype');
    }
    @font-face {
      font-family: 'Roboto Italic';
      font-style: normal;
      font-weight: 300;
      src:url(fonts/Roboto-Italic.ttf) format('truetype');
    }
    .font-missing {
      color:red;
      font-size:25px;
    }
    .robotoitalic-loading {
      color: green;
    }
    .robotoitalic-loaded {
      font-size:25px;
      color: blue;
    }
    .robotoitalic-missing {
      color: red;
    }
    .robotoitalic {
      font-family: 'Roboto Italic';
    }
  </style>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
</head>
<body>
<h1>Google AMP - Amp Font</h1>
<amp-font layout="nodisplay"
          font-family="Font Does Not exist"
          timeout="2000"
          on-error-remove-class="font-missing"
          on-error-add-class="font-error"
          on-load-remove-class="font-missing"
          on-load-add-class="font-loaded">
</amp-font>
<p class="font-missing">
  로드할 글꼴 파일이 작동할 때 on-error-remove-class, on-error-add-class, on-load-remove-class 및 on-load-add-class 속성이 작동하는 방식을 보여주는 amp 글꼴 구성 요소의 예는 존재하지 않습니다.
</p>
<amp-font layout="nodisplay"
          font-family="Roboto Italic"
          timeout="2000"
          on-error-remove-class="robotoitalic-loading"
          on-error-add-class="robotoitalic-missing"
          on-load-remove-class="robotoitalic-loading"
          on-load-add-class="robotoitalic-loaded">
</amp-font>
<p class="robotoitalic">
  글꼴 파일이 존재하고 정상적으로 로드될 때 on-error-remove-class, on-error-add-class, on-load-remove-class 및 on-load-add-class 속성이 작동하는 방식을 보여주는 amp-font 구성 요소의 예.
</p>
</body>
</html>

결과

위에 제공된 샘플 코드의 출력은 다음과 같습니다.

위의 예제는 font-family, timeout, on-error-remove-class, on-error-add-class, on-load-remove-class, on-load-add.class와 같은 글꼴 속성으로 작업하는 방법을 보여줍니다. 클래스는 글꼴 로드에 오류 또는 성공 여부를 결정합니다.

방문해주셔서 감사합니다. 즐거운 하루 되세요!

관심 있을 만한 글

  • AMP에는 유용한 구성 요소인 amp-carousel이 있습니다. 주로 이미지 및 갤러리와 함께 사용되지만 스크롤 가능한 내비게이션 메뉴를 만드는 데도 사용할 수 있습니다. 먼저 스크립트를 가져와야 합니다. 문서 헤드에 다음을 추가합니다. <script as…
  • 탭 콘텐츠는 페이지 내에서 이동하지 않고 탭을 전환하여 다양한 보기 방식를 제공합니다. 탭으로 구분된 콘텐츠는 amp-selector를 사용하여 구현할 수 있습니다. 사용자가 선택할 수 있는 옵션의 목록을 표시하는 구성 요소입니다. 예를 들면 전자 상거래에서는 제품…
  • amp-component에서 액션이나 이벤트를 사용하려면 on 속성을 사용할 수 있습니다. 이 글에서는 이에 대해 자세히 설명하겠습니다. Events 이벤트에 대한 액션 구문은 다음과 같습니다. on="eventName:elementId[.methodName[(arg1=value, arg2=value…
  • amp-dynamic-css-classes는 body 태그에 동적 클래스를 추가합니다. 이 글에서는 이 태그에 대해 자세히 알아보겠습니다. amp-dynamic-css-classes로 작업하려면 다음 스크립트를 추가해야 합니다. <script async custom-element="amp-dyn…
  • 구글 amp-user-notification은 사용자에게 닫을 수 있는 대화 상자 메시지를 표시하는데 사용됩니다. 우리는 페이지의 쿠키에 대해 사용자에게 알리는데 사용할 수 있습니다. amp-user-notification을 사용하려면 페이지에 다음 스크립트를 추가해야 합니다. <sc…
  • Amp-animation은 다른 amp 구성 요소에서 사용할 애니메이션을 정의하는 amp 구성 요소입니다. 이 글에서는 이에 대해 자세히 설명합니다. Amp-animation을 사용하려면 다음 스크립트를 추가해야 합니다. <script async custom-element="amp-animati…

댓글 쓰기