javascript - How to ensure that custom font is loaded -
i'm using angular 2 , webpack. styles added angular components without encapsulation.
there problem custom fonts - seems loaded asynchronously, components, whic mesuring there size @ moment created, initial font instead of custom.
how can solve problem?
as understand, there no helpful events... thought checking currecnt font , setting dela, didn't find way chech current font of element. computed value returned if such font not applied.
or maybe angular/webpack-specific solution can applied?
~function () { var link = document.createelement('link'); link.rel = 'stylesheet'; link.href = 'https://fonts.googleapis.com/css?family=shrikhand&_='+date.now(); document.head.appendchild(link); var dest = document.getelementbyid('dest'); console.log(getcomputedstyle(dest).fontfamily); var width = getcomputedstyle(dest).width; dest.style.width = width; }(); body { font-family: 'shrikhand', cursive; } p { outline: 1px dotted red; float: left; clear: left; white-space: nowrap; } <p style="font-family: initial;">need handle font loading</p> <p id="dest">need handle font loading</p> <p>need handle font loading</p>
you can use font face observer. monitor when web font applied page , notify you. currently, it's best possible solution determine whether web font has been loaded.
if interested in subject check out zach leatherman's blog. wrote comprehensive guide on topic :)
Comments
Post a Comment