在网站中使用谷歌“ROBOTO”字体(解决ios数字1和0大小不等宽问题)

在网页开发中,为了解决iOS设备上数字1和0显示宽度不一致的问题,采用谷歌的ROBOTO字体。通过下载字体包,上传到网站并添加CSS3 @font-face规则,实现跨平台字体兼容,特别是在iPhone 5s上的应用。本文提供了解决方案的详细步骤和常见问题的排查方法。

在网站中使用谷歌“ROBOTO”字体(解决ios数字1和0大小不等宽问题)

在开发中发现在mac上数字0和1所占的宽度不一样,网上查询需要用到等宽字体,但是浏览器支持的默认几种等宽字体中文很难看,随即引入谷歌的字体进行渲染。

最近在写一个移动端的web项目,designer设计的页面效果图就是使用Google Roboto Font。The Roboto Font 是Google为Android 4.0 Ice-Cream Sandwich platform而设计的,是一个新的san-serif字集。

在css中,我直接使用了如下的代码:

body{font-family:'Roboto',Roboto Lt;}

页面测试时,Roboto字体在Android Platform下面是渲染出来了,但是在iphone 5s下面则出现字体不生效的情况。

我直接按照Google官方的quick example code来调用这个Google的在线字体库,但是发现还是没有效果,同时也在stackoverflow发了问题。

后来,在Google Search Engine中找到了这篇文章:How to Use The Google’s Roboto Font Everywhere

你可以直接看上面链接中的文章,我的解决办法就是按照文中的思路来实现的。如果你看不懂英文,那么你可以看看我下面的中文解决步骤。

  1. 下载字体包,链接是 FontSquirrel Roboto font page。

  2. 在打开的页面中,选择“Webfont kit”,然后你点击打开的选项页下面的"DOWNLOAD@FONT-FACE KIT"进行压缩包下载。

  3. 下载完成之后,解压到当前文件夹,你可以看到解压出来的web fonts目录,打开该目录你会发现里面很多都是Roboto的各种字体。你直接选择一个你想要用在web size的上传到你的网站相关目录下面即可,当然,你还必须要添加一段css3 @font-face代码来进行嵌入扩展的字体到你的站点。代码如下:(注意,src path必须是根据你个人的具体情况来定,不是下面写死的代码。)

  4. [复制代码](javascript:void(0)?

     1 @font-face {
     2     font-family: 'Roboto';
     3     src: url('Roboto-Regular-webfont.eot');
     4     src: url('Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
     5          url('Roboto-Regular-webfont.woff') format('woff'),
     6          url('Roboto-Regular-webfont.ttf') format('truetype'),
     7          url('Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
     8     font-weight: normal;
     9     font-style: normal;
    10 }
    11 
    12 //假如你想要扩展更多的字体库,那么你就可以按照上面代码来进行引用就行。
    

官方示例

Installing Webfonts

Webfonts are supported by all major browser platforms but not all in the same way. There are currently four different font formats that must be included in order to target all browsers. This includes TTF, WOFF, EOT and SVG.

1. Upload your webfonts

You must upload your webfont kit to your website. They should be in or near the same directory as your CSS files.

2. Include the webfont stylesheet

A special CSS @font-face declaration helps the various browsers select the appropriate font it needs without causing you a bunch of headaches. Learn more about this syntax by reading the Fontspring blog post about it. The code for it is as follows:

@font-face{ 
	font-family: 'MyWebFont';
	src: url('WebFont.eot');
	src: url('WebFont.eot?iefix') format('eot'),
	     url('WebFont.woff') format('woff'),
	     url('WebFont.ttf') format('truetype'),
	     url('WebFont.svg#webfont') format('svg');
}

We’ve already gone ahead and generated the code for you. All you have to do is link to the stylesheet in your HTML, like this:

<link rel="stylesheet" href="stylesheet.css" type="text/css" charset="utf-8" />

3. Modify your own stylesheet

To take advantage of your new fonts, you must tell your stylesheet to use them. Look at the original @font-face declaration above and find the property called “font-family.” The name linked there will be what you use to reference the font. Prepend that webfont name to the font stack in the “font-family” property, inside the selector you want to change. For example:

p { font-family: 'MyWebFont', Arial, sans-serif; }

4. Test

Getting webfonts to work cross-browser can be tricky. Use the information in the sidebar to help you if you find that fonts aren’t loading in a particular browser.

Troubleshooting Font-Face Problems

Having trouble getting your webfonts to load in your new website? Here are some tips to sort out what might be the problem.

Fonts not showing in any browser

This sounds like you need to work on the plumbing. You either did not upload the fonts to the correct directory, or you did not link the fonts properly in the CSS. If you’ve confirmed that all this is correct and you still have a problem, take a look at your .htaccess file and see if requests are getting intercepted.

Fonts not loading in iPhone or iPad

The most common problem here is that you are serving the fonts from an IIS server. IIS refuses to serve files that have unknown MIME types. If that is the case, you must set the MIME type for SVG to “image/svg+xml” in the server settings. Follow these instructions from Microsoft if you need help.

Fonts not loading in Firefox

The primary reason for this failure? You are still using a version Firefox older than 3.5. So upgrade already! If that isn’t it, then you are very likely serving fonts from a different domain. Firefox requires that all font assets be served from the same domain. Lastly it is possible that you need to add WOFF to your list of MIME types (if you are serving via IIS.)

Fonts not loading in IE

Are you looking at Internet Explorer on an actual Windows machine or are you cheating by using a service like Adobe BrowserLab? Many of these screenshot services do not render @font-face for IE. Best to test it on a real machine.

Fonts not loading in IE9

IE9, like Firefox, requires that fonts be served from the same domain as the website. Make sure that is the case.

参考:https://www.cnblogs.com/shuman/p/3803608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值