views:

6752

answers:

5

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

+4  A: 

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

A: 

Its not working. What am i doing wrong here?

Code:

NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body { \n"
"font:helvetica; \n"
"}\n"   
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", myDesc];

NSLog(myDescriptionHTML);
[myWebView loadHTMLString:myDescriptionHTML baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

Here's the result of NSLog:

HTML Code:

<html> 
<head> 
<style type="text/css"> 
body { 
font:Helvetica; 
}
</style> 
</head> 
<body>Anointed by the games media as <a href="http://www.allgame.com/cg/agg.dll?p=agg&amp;amp;sql=32:Microsoft"&gt;Microsoft&lt;/a&gt;'s first &quot;killer app&quot; for its <a href="http://www.allgame.com/cg/agg.dll?p=agg&amp;amp;sql=5:47504"&gt;Xbox 360</a>, <a href="http://www.allgame.com/cg/agg.dll?p=agg&amp;amp;sql=32:Epic?Games"&gt;Epic Games</a>' <a href="http://www.allgame.com/cg/agg.dll?p=agg&amp;amp;sql=1:47510"&gt;Gears of War</a> is a futuristic third-person shooter emphasizing tactical action over run-and-gun battles.</body> 
</html>

Dreamweaver seems to be showing the correct font. Any ideas?

Mustafa
Do not use the Answer box to ask a question. Edit your original question or Ask a new question.
benzado
I wasn't familiar with stackoverflow at the time. If you notice, this post is from Jan 09.
Mustafa
Posts live on forever. Take a moment to tidy up and make the world a better place. :-)
benzado
+1  A: 

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
+2  A: 

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
A: 

just change

"font:helvetica; \n"

to

"font-family:helvetica; \n"

Rob