How to change UIWebView or WKWebView default font
You can use your UIFont
object (so you can set it and modify more easily), but wrap the HTML in a span
instead; font
tags have been deprecated since HTML 4.01.
UIFont *font = [UIFont fontWithName:@"GothamRounded-Bold" size:14];
Assuming you already have the NSString *htmlString
created, you can use the font's properties:
htmlString = [NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %i\">%@</span>",
font.fontName,
(int) font.pointSize,
htmlString];
Alternatively, you could just supply the values instead of using a UIFont
object:
htmlString = [NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %i\">%@</span>",
@"GothamRounded-Bold",
14,
htmlString];
Just prefix a <font face>
tag to your string before loading into the webView.
NSString *body = [plist objectForKey:@"foo"];
NSString *htmlString =
[NSString stringWithFormat:@"<font face='GothamRounded-Bold' size='3'>%@", body];
[webView loadHTMLString:htmlString baseURL:nil];
You could try to set the font by injecting a line of JavaScript like this:
[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.fontFamily = 'GothamRounded-Bold'"];