2017-04-13 66 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。

相关问题