Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Created September 10, 2021 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmr/71850bde682a06626c698a3eb652d8cc to your computer and use it in GitHub Desktop.
Save ryanmr/71850bde682a06626c698a3eb652d8cc to your computer and use it in GitHub Desktop.

Changing the font in a mapboxgl map is really only possible by using a font in the Mapbox Studio system.

      // map.current.setLayoutProperty(
      //   "country-label",
      //   "text-size",
      //   originalSize * Number(e.target.value)
      // );
      // map.current.getLayoutProperty()
      console.log(map.current);
      const labelTypes: string[] = map.current.style._order.filter((a) =>
        a.includes("label")
      );
      console.log("labelTypes", labelTypes);
      // console.log(map.current);
      labelTypes.forEach((labelType: string) => {
        const existing = map.current?.getLayoutProperty(
          labelType,
          "text-field"
        );
        console.log("existing value for", labelType, existing);
        // map.current?.setLayoutProperty(labelType, "text-field", [
        //   "format",
        //   // ["get", "name_en"],
        //   // { "font-scale": 1.2 },
        //   // "\n",
        //   // {},
        //   ["get", "name"],
        //   {
        //     "text-font": [
        //       "literal",
        //       ["DIN Offc Pro Italic", "Arial Unicode MS Regular"],
        //     ],
        //   },
        // ]);
        map.current?.setLayoutProperty(labelType, "text-font", [
          "spirit-condensed",
          "DIN Offc Pro Italic",
          "Arial Unicode MS Regular",
        ]);
      });

This was a way to hack around it, but it does not work because the fonts are indexed from Mapbox servers on their assumption that you only use their assets. Fonts from foundaries might not allow this usage pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment