Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Default font in a WinUI3 application (WebView2) #721

Closed
BertrandDeSaintBezier opened this issue Jul 5, 2023 · 2 comments
Closed
Labels

Comments

@BertrandDeSaintBezier
Copy link

Hi :)

I'm using Markdig in a WinUI3-type framework to spit out .md files as .html in a WebView2 UIElement. The text comes out in a font that doesn't match well with the rest of the application. I was wondering how you would go about changing the default font (and other properties ?). I'm guessing something to do with ResourceDictionaries ? Unsure how to proceed.

Thanks !

@xoofx xoofx added the question label Jul 5, 2023
@xoofx
Copy link
Owner

xoofx commented Jul 5, 2023

Markdig only generates a HTML fragment, not a full page, so you should have control over the HTML page (the head+body) and where you insert this fragment and then you can change CSS properties the way you want, as for a regular HTML page.

@BertrandDeSaintBezier
Copy link
Author

Wonderful, thanks very much !

I was using this in a WebView2 so I don't know if this is the best way of going about things, but I essentially just concatenated some css styling to the front of the converted markdown :

private async void WebView_Loaded(object sender, RoutedEventArgs args)
{
    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, @"Sample.md");
    var text = File.ReadAllText(path);
    var result = Markdown.ToHtml(text);
    var styledResult = String.Concat("<style>\r\n" +
                                        "html * {\r\n" +
                                            "font-size: 16px;\r\n" +
                                            "line-height: 1.625;\r\n" +
                                            "color: #2020131;\r\n" +
                                            "font-family: Nunito, sans-serif;\r\n" +
                                        "}\r\n" +
                                     "</style>", 
                                     result);

    await WebView.EnsureCoreWebView2Async();
    WebView.NavigateToString(styledResult);
}

This ended up working perfectly as a starting point. I'm still fiddling around with things before I settle on the nicest way of doing it.

Hope this helps someone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants