IPhone Development - Customizing the UIWebView Font

I need to show rich text that I pull from the server, so I use UIWebView. Now the problem is that I do not control the font that is used in UIWebView. How can I change the font to use the system font (which is consistent with the rest of the application)?

I am 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, you need to control the display font. If I can't control the font, please let me know other alternatives to UIWebView.

thank

+44
iphone uiwebview
Jan 16 '09 at 7:42
source share
9 answers

Link to a CSS table in an HTML document or place style information at the top of the HTML document itself

+22
Jan 16 '09 at 8:23
source share

It worked when I modified a string like this. Your right chris, that shouldn't matter.

 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]; 
+103
Jan 17 '09 at 6:10
source share

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

 body { font-family:helvetica; } 

But yours looks like it should work too. It’s strange. The only difference I see is the font vs font-family, but that should not change.

Chris.

+14
Jan 16 '09 at 14:44
source share

System font

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

 font-family: -apple-system; font-family: '-apple-system','HelveticaNeue'; // iOS 8 compatible 
+11
Dec 08 '15 at 4:50
source share

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

It is used as follows:

 NSString * htmlString = [MyClass htmlFromBodyString:@"an <b>example</b>" textFont:[UIFont systemFontOfSize:10] textColor:[UIColor grayColor]]; [webView loadHTMLString:htmlString baseURL:nil]; 
+10
Jan 30 '14 at 15:43
source share

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]; 
+6
Jul 04 '14 at 7:50
source share

just change

font:helvetica

to

font-family:helvetica

+4
09 Feb '10 at 4:19
source share

I use

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

This is great for my about-page (WebView), as the rest of my application also uses Apple Notably Bold.

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

+2
Feb 05 2018-12-12T00:
source share

Here is my easy to use and easily extensible solution with lots of 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]; 
+2
May 18 '14 at 2:58
source share



All Articles