简体   繁体   中英

How to change Custom Font in UIWebView?

I tried

This answer

I loaded m.facebook.com into my UIWebView, which I'd like to change to a custom font. However, the font did not change. I believe the above answer is for static HTML Page.

I'm new to iOS, so a detailed explanation would be appreciated.

The documentation is here, which is Safari CSS Reference

You can also try this:

NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                            "<head> \n"
                            "<style type=\"text/css\"> \n"
                            "body {font-family: \"%@\"; font-size: %@;}\n"
                            "</style> \n"
                            "</head> \n"
                            "<body>%@</body> \n"
                            "</html>", @"helvetica", [NSNumber numberWithInt:kFieldFontSize], content];

Ok, so what we did was basically declared an NSString . We call it myDescriptionHTML . The string basically has HTML code/structure that, in a nutshell, changes the body font to helvetica. And the [NSNumber numberWithInt:kFieldFontSize], content] basically specifies that the font size. Then:

[webView loadHTMLString:myDescriptionHTML baseURL:nil];

Now our UIWebView is calling the method loadHTMLString: baseURL:

The loadHTMLString: takes a parameter that has a HTML structure, myDescriptionHTML is perfect, we just declared that to load the HTML page with a specific font. baseURL parameter will be needed for something like an image, but we won't need that, unless your font is not supported/you created it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM