How to change UIWebView default font

What font does UIWebView use by default? I would like to be able to change that. But I don't want to do it in the html string, instead I want to have something like:

[webView setFont:[UIFont fontWithName:@"GothamRounded-Bold" size:14]

is there such property or some other way?


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 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>",
                                        (int) font.pointSize,

Alternatively, you could just supply the values instead of using a UIFont object:

htmlString = [NSString stringWithFormat:@"<span style=\"font-family: %@; font-size: %i\">%@</span>",

You could try to set the font by injecting a line of JavaScript like this:

[webView stringByEvaluatingJavaScriptFromString: @" = 'GothamRounded-Bold'"];

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 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];"
                        "<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];

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.

Need Your Help

Enable UNIQUE CONSTRAINT = Violation

oracle oracle11g oracle10g

When enabling a unique constraint I get a violation.

Best way to compare IP addresses quickly

android algorithm ip-address

I'm parsing two CSV files which contains IP addresses.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.