在网站中使用谷歌“ROBOTO”字体(解决ios数字1和0大小不等宽问题)
在網站中使用谷歌“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
你可以直接看上面鏈接中的文章,我的解決辦法就是按照文中的思路來實現的。如果你看不懂英文,那么你可以看看我下面的中文解決步驟。
下載字體包,鏈接是 FontSquirrel Roboto font page。
在打開的頁面中,選擇“Webfont kit”,然后你點擊打開的選項頁下面的"DOWNLOAD@FONT-FACE KIT"進行壓縮包下載。
下載完成之后,解壓到當前文件夾,你可以看到解壓出來的web fonts目錄,打開該目錄你會發現里面很多都是Roboto的各種字體。你直接選擇一個你想要用在web size的上傳到你的網站相關目錄下面即可,當然,你還必須要添加一段css3 @font-face代碼來進行嵌入擴展的字體到你的站點。代碼如下:(注意,src path必須是根據你個人的具體情況來定,不是下面寫死的代碼。)
[](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
總結
以上是生活随笔為你收集整理的在网站中使用谷歌“ROBOTO”字体(解决ios数字1和0大小不等宽问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JVM架构和GC垃圾回收机制详解
- 下一篇: 小说里的lt什么意思_游戏cpdd网络用