46

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

Mustafa
  • 20,504
  • 42
  • 146
  • 209

9 Answers9

103

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];
Mustafa
  • 20,504
  • 42
  • 146
  • 209
  • 2
    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... – Deepukjayan Mar 14 '12 at 05:41
  • 15
    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` – DBD Sep 04 '12 at 14:26
  • 1
    @DBD since iOS9, the `fontName` will be something like `.SFUIText-Regular`, which doesn't work with HTML – Daniel Jul 18 '16 at 14:41
22

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

15

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.

PyjamaSam
  • 10,437
  • 3
  • 32
  • 20
  • 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 – B-Money May 24 '13 at 03:39
11

System Font

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

font-family: -apple-system;
font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible
Leslie Godwin
  • 2,601
  • 26
  • 18
10

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];
Community
  • 1
  • 1
Robert
  • 37,670
  • 37
  • 171
  • 213
6

another easy way that worked for me

    UIFont *font = [UIFont systemFontOfSize:15];

      NSString *htmlString = [NSString stringWithFormat:@"<span style=\"font-family:Helvetica; font-size: %i\">%@</span>",
                     (int) font.pointSize,
                     [bodyText stringWithNewLinesAsBRs]];
       [myWebView loadHTMLString:htmlString baseURL:nil];
Mashhadi
  • 3,004
  • 3
  • 46
  • 80
4

just change

font:helvetica

to

font-family:helvetica

John Conde
  • 217,595
  • 99
  • 455
  • 496
Rob
  • 520
  • 5
  • 17
2

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];
Al-Noor Ladhani
  • 2,413
  • 1
  • 22
  • 14
2

I use

body {
        font-family: 'Noteworthy-Bold';
    }

This works fine for my about-page (WebView), as the rest of my app also uses Apple's "Noteworthy-Bold".

If you want to use your own font, make use of @font-face and refer to a TTF or EOT file.

Michael Biermann
  • 3,105
  • 27
  • 23