Reputation: 12079
I am trying to use the very same font in a UIWebView on iOS that the iOS system uses currently.
This means I want to use the very same font I'd get from:
UIFontDescriptor *fd;
fd = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFont *font = [UIFont fontWithDescriptor:fd size:0];
There are lots of answers on SO (iPhone Development - Setting UIWebView font, Using custom font in a UIWebView, How to change UIWebView default font, How to set custom font in UIWebView?) that explain how to embed a UIFont's familyName or fontName into an html's style attributes, by doing this:
NSString *fontStyle, *htmlString = @"test";
fontStyle = [NSString stringWithFormat:@"font-family:%@;font-size:%d", font.familyName, (int)font.pointSize];
htmlString = NSString stringWithFormat:@"<span style=\"%@\">%@</span>", fontStyle, htmlString];
But this doesn't seem to work on iOS 7 and 8 any more where these system font names are now like:
font.fontName: .HelveticaNeueInterface-Regular
font.familyName: .Helvetica Neue Interface
When using these names in an UIWebView with the "font-family" attribute, the text gets rendered in the default font (which is with serifs and certainly not the new iOS system font). Removing the leading period doesn't help, either.
So, how do I now translate the system's font into a name that gets correctly interpreted by the Web Kit?
Please understand that I am seeking for a generic solution and do not simply seek the iOS 7/8 name I can insert manually into the html style attribute.
Upvotes: 2
Views: 1102
Reputation: 12079
I just ran into this blog article which may offer the solution though I have not verified it yet:
If you're trying to reference the system font in web views just prepend
-apple-system
to your font-family list and it'll use SF in iOS 9 and Helvetica in older versions.
Which means that the font line of above code needs to be change to:
fontStyle = [NSString stringWithFormat:@"font-family:-apple-system,Helvetica Neue;font-size:%d", (int)font.pointSize];
That should take care of selecting the system font from iOS 7 onward.
Upvotes: 3
Reputation: 4807
Have you tried Helvetica or San Francisco with attaching font files?
Upvotes: 0