how to apply custom font to web browser content WP7

前端 未结 2 1493
面向向阳花
面向向阳花 2020-12-20 08:22

Am developing simple app for learning webBrowser concept in Windows phone. My aim is to display the content in Telugu(Indian language) in my WP7. that web a

相关标签:
2条回答
  • 2020-12-20 09:06

    Windows phone supports web fonts. However, they cannot be embedded in the XAP. Suggested workaround is to host the fonts on a remote server and perhaps use AppCache to keep the font files locally on the device.

    0 讨论(0)
  • 2020-12-20 09:12

    No need to host from remote server, you can use local font embedded CSS and inject to browser control.

    (1.) Create the CSS with embedded font

    body, a, p, span, div, textarea {
            font-family: MyCustomFont;
    }
    
    @font-face {
        font-family: MyCustomFont;
        src: url(data:application/x-font-woff;charset=utf-8;base64,d09GRgABAXXXXXXXXXXX......) format('woff');
        font-weight: normal;
        font-style: normal;
    } 
    

    (2.) Inject jQuery and CSS at WebView_NavigationCompleted method

     private async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
     {
        // Inject jQuery file which located at local
        StorageFile jquery = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///JavaScripts\\jquery-1.10.2.min.js"));
            string jquery_string = await FileIO.ReadTextAsync(jquery);
            await MyWebView.InvokeScriptAsync("eval", new string[] { jquery_string });
    
    
        // Inject custom font embedded CSS 
        StorageFile myfontcss = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///CSS\\mycustomfont.css"));
                string myfontcss_string = await FileIO.ReadTextAsync(myfontcss);
    
                myfontcss_string = myfontcss_string.Replace("\n", "").Replace("\t", ""); //.Replace("'", "'").Replace("\"", """);
                string cssRef = "$(\"<style id='myfont' type='text/css'>" + myfontcss_string + "</style>\").appendTo('head');";
    
                await MyWebView.InvokeScriptAsync("eval", new string[] { cssRef });
    
     }
    
    0 讨论(0)
提交回复
热议问题