What font does UIWebView
and WKWebView
use by default? I would like to be able to change that. But I don\'t want to do it in the html string, inste
For Swift, I use this one in webViewDidFinishLoad
webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('body')[0].style.fontFamily =\"HelveticaNeue\"");
UIWebview
uses the font that's set in the HTML. There is not 'default font'. Even if no font is specifically mentioned, you do not have access to set it. It's all inside the WebKit, which we don't have access to.
In case someone is looking for a solution in Swift 3, I used this (based on jterry's answer):
let font = UIFont.init(name: "Name-of-your-font", size: theSize)
self.newsWebView.loadHTMLString("<span style=\"font-family: \(font!.fontName); font-size: \(font!.pointSize); color: #21367B\">\(yourString)</span>", baseURL: nil)
Be aware that here I'm not making sure the font isn't nil. A nil check could be added before loading the HTML string. Here I'm also changing the color of the font by adding the color option in the style of the span. This is completely optional.
Here is my easy-to-use and easy-to-expand solution with more font settings:
myHTMLLabel = [[UIWebView alloc] initWithFrame:CGRectMake(myX, myY, myWidth, myHeight)];
myHTMLLabel.userInteractionEnabled=NO;
myHTMLLabel.scalesPageToFit=NO;
[myHTMLLabel setOpaque:NO];
myHTMLLabel.backgroundColor = myBackColor;
NSString *myHTMLText = [NSString stringWithFormat:@"<html>"
"<head><style type='text/css'>"
".main_text {"
" display: block;"
" font-family:[fontName];"
" text-decoration:none;"
" font-size:[fontSize]px;"
" color:[fontColor];"
" line-height: [fontSize]px;"
" font-weight:normal;"
" text-align:[textAlign];"
"}"
"</style></head>"
"<body> <SPAN class='main_text'>[text]</SPAN></body></html>"];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[text]" withString: myText];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontName]" withString: myFontName];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontSize]" withString: myFontSize];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[fontColor]" withString: myFontColorHex];
myHTMLText = [myHTMLText stringByReplacingOccurrencesOfString: @"[textAlign]" withString: myFontAlign];
NSLog(@"*** renderHTMLText --> myHTMLText: %@",myHTMLText);
[myHTMLLabel loadHTMLString:myHTMLText baseURL:nil];
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];
You could try to set the font by injecting a line of JavaScript like this:
[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.fontFamily = 'GothamRounded-Bold'"];