简体   繁体   English

如何显示谷歌字体预览

[英]How to show Google font preview

I am loading Google font-families name into a select-list.我正在将 Google 字体系列名称加载到选择列表中。 When user select any font-family from the list, then I load that font dynamically.当用户从列表中选择任何字体系列时,我会动态加载该字体。 My code:我的代码:

$.get("https://www.googleapis.com/webfonts/v1/webfonts?key=...", {}, function (data) {
    $.each(data.items, function (index, value) {
        $('#fc').append($("<option></option>").attr("value", value.family)
                .text(value.family));
    });
    $('#fc').selectmenu({ select: function () {
        $('body').append("<link rel='stylesheet' id='colorbox-css'  href='http://fonts.googleapis.com/css?family=" + escape($(this).selectmenu("value")) + "' type='text/css' media='all' />");
    }
    });
});

Now I want to show the preview of the font to the user before loading that font.现在我想在加载该字体之前向用户显示该字体的预览。

Is this possible?这可能吗?

I ran into this challenge myself, so used some console action and open sourced the following: https://github.com/onassar/GoogleWebFonts-FontFamilyPreviewImages我自己遇到了这个挑战,所以使用了一些控制台操作并开源了以下内容: https ://github.com/onassar/GoogleWebFonts-FontFamilyPreviewImages

I'll be pretty active in maintaining it (just gotta write up the docs now).我会非常积极地维护它(现在只需要写文档)。

Try HiGoogleFonts .试试HiGoogleFonts It shows all the google fonts in a drop down with a preview of each.它在下拉列表中显示了所有谷歌字体,并提供了每种字体的预览。 it only loads the font which is selected.它只加载所选的字体。 It uses a single image for fonts preview.它使用单个图像进行字体预览。

You could load the fonts into your page as described here .您可以按照此处所述将字体加载到您的页面中 Then simply have a div with your sample text and style it based on the font chosen from the drop-down.然后只需使用您的示例文本创建一个 div,并根据从下拉列表中选择的字体设置样式。

Specifically look at the last section on adding event handlers - you can use JS to detect when font(s) have finished loading and apply them as appropriate具体看最后一节关于添加事件处理程序——你可以使用 JS 来检测字体何时完成加载并适当地应用它们

This github project comes to mind.我想到了这个 github 项目。 It too offers the user a preview of google fonts Fontselect jQuery Plugin它还为用户提供了谷歌字体Fontselect jQuery 插件的预览

I tackled this problem myself recently when adding Google Fonts API functionality to have a constantly updated select list of currently available Google fonts.我最近在添加 Google Fonts API 功能时自己解决了这个问题,以便不断更新当前可用的 Google 字体的选择列表。 I used the jQuery Fontselect Plugin as a base but I modified it quite a bit to fit my purpose.我使用jQuery Fontselect 插件作为基础,但我对其进行了相当多的修改以适应我的目的。 I explain how I tackled it here: https://www.bibleget.io/2020/04/released-v5-3-of-the-wordpress-plugin/ Basically I'm using some sophisticated cURL requests to the Google Fonts API in order to download locally a reduced version of each font with only the characters that are used for the name of the font, and then preloading those reduced versions of the fonts onto the page that will have the select dropdown.我在这里解释了我是如何解决这个问题的: https ://www.bibleget.io/2020/04/released-v5-3-of-the-wordpress-plugin/ 基本上我对 Google Fonts API 使用了一些复杂的 cURL 请求为了在本地下载每种字体的简化版本,其中仅包含用于字体名称的字符,然后将这些字体的简化版本预加载到将具有选择下拉列表的页面上。 You can see it in action here: https://youtu.be/zqJqU_5UZ5M你可以在这里看到它的实际效果: https ://youtu.be/zqJqU_5UZ5M

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

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