iPhone Development - Setting UIWebView font

61,199

Solution 1

Reference a css style sheet in your HTML document or put the style information into the top of the HTML document itself

Solution 2

It worked when i modified the string like this. Your right chris, it shouldn't make any difference.

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

Solution 3

I use the following CSS in a web view to do exactly what you are saying.

body
{
    font-family:helvetica;
}

But yours looks the like it should work as well. Strange. Only difference I can see is the font vs font-family, but that shouldn't make a difference.

chris.

Solution 4

System Font

-apple-system on iOS 9 & Safari OS X 10

font-family: -apple-system;
font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible

Solution 5

I made a method that wraps some text in a HTML body with the correct style for a given UIColor and UIFont. It is based on Mustafa answer.

It is used like this:

NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>"
                                           textFont:[UIFont systemFontOfSize:10]
                                          textColor:[UIColor grayColor]];
[webView loadHTMLString:htmlString baseURL:nil];
Share:
61,199

Related videos on Youtube

Mustafa
Author by

Mustafa

I'm a Manager Development/Project Manager/Team Lead/Mobile Application Developer located in Islamabad, Pakistan, working for BroadPeak Technologies. I'm currently focusing on managing and developing mobile applications for Android and iOS devices; with hands on experience developing iOS applications. More information.

Updated on July 09, 2022

Comments

  • Mustafa
    Mustafa almost 2 years

    I have to show rich text that i pull from server, so i'm using UIWebView. Now the problem is that i have no control over the font which is used in UIWebView. How can i change the font to use system font (making it consistent with rest of the application)?

    I'm doing something like this at the moment:

    myRichTextView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    myRichTextView.backgroundColor = [UIColor clearColor];
    [myRichTextView setScalesPageToFit:NO];
    [myRichTextView loadHTMLString:myString baseURL:[NSURL URLWithString:myBaseURL]];
    

    Again, need to control the display font. If i can't control the font, please let me know other alternatives to UIWebView.

    Thanks

    • Ethan Parker
      Ethan Parker about 8 years
      if you're pulling from a server, what's the string for myString? Wouldn't all the text come from the server?
  • Deepukjayan
    Deepukjayan about 12 years
    ok thats right. Just another doubt, which HTML-font is equivalent to "SystemFont" in ios? Hope its not a big question to be posted as a new question...
  • DBD
    DBD over 11 years
    I keep waiting for Apple to let users change the system font. so I go just a tiny step further. I do a UIFont *font = [UIFont systemFontOfSize:21]; and instead of hard coding @"helvetica" I put in font.familyName
  • B-Money
    B-Money almost 11 years
    Yep this works. And also if you have a space in the font's name, you need to put it in quotes, such as "helvetica neue", san-serif
  • benLIVE
    benLIVE over 9 years
    Thanks for the method! @Robert it's useful!
  • Apfelsaft
    Apfelsaft over 9 years
    Works perfectly. Thanks!
  • Daniel
    Daniel almost 8 years
    @DBD since iOS9, the fontName will be something like .SFUIText-Regular, which doesn't work with HTML