2017-04-13 20 views
3

我正在使用特殊字體來顯示內容。它適用於大多數設備,但不適用於iOS設備。所以我需要改變字體系列CSS當用戶有一個蘋果設備,但其他人顯示正常。我怎樣才能做到這一點?如果用戶的設備是iOS,則如何更改字體系列css

我已經附加了一些僞代碼:接近這個任務

if(IOS) { html,body,h1,h2,h3,h4,h5 { line-height: 150%; font-family: "Open Sans", sans-serif} 
}else{ 
html,body,h1,h2,h3,h4,h5 { line-height: 150%; font-family: "Noto Sans Sinhala", sans-serif} 
} 

回答

3

一種方法是使用用戶代理嗅探技術,就像這樣:

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; 

因此,在實踐中這將是:

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; 

if (iOS) { 
    // iOS specific styles 
} 
else { 
    // other styles 
} 

我建議你在樣式表文件中包含所有樣式,只包含fon T系列(並且只有樣式根據設備而變化)。所以,你的完整代碼可能看起來是這樣的(假設這JavaScript是在HTML文件的頭部):

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; 

if (iOS) { 
    document.write("<style>body { font-family: x }</style>"); 
} 
else { 
    document.write("<style>body { font-family: y }</style>"); 
} 

你可以閱讀更多關於this answer檢測的iOS。

相關問題