iPhone Development - Setting UIWebView font
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];
Related videos on Youtube
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, 2022Comments
-
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 about 8 yearsif you're pulling from a server, what's the string for myString? Wouldn't all the text come from the server?
-
-
Deepukjayan about 12 yearsok 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 over 11 yearsI 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 infont.familyName
-
B-Money almost 11 yearsYep 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 over 9 yearsThanks for the method! @Robert it's useful!
-
Apfelsaft over 9 yearsWorks perfectly. Thanks!
-
Daniel almost 8 years@DBD since iOS9, the
fontName
will be something like.SFUIText-Regular
, which doesn't work with HTML