Using a UIWebView, can I use a custom font?

14,470

Solution 1

It is possible to load custom fonts into your UIWebView in iOS3.2 and above. Add the font to your bundle (see here) then just reference the font in your UIWebView's stylesheet like you would any other font:

<style type='text/css'>font { font-family: DroidSerif; } </style>

Solution 2

It is possible, but you need to make certain to add the Fonts provided by application key-value pair to the Info.plist.

I learned about it in this tutorial: http://tetontech.wordpress.com/2010/09/03/using-custom-fonts-in-your-ios-application/

I tried it with a LWFM and a FFIL font suitcase, but this didn't work. In the end I needed the TTF file of the font.

When referencing the font in your code use the PostScript name. To get the correct name for your font, open the Font Book application, select the font and click on the i in the upper left corner to open the information panel.

Fontbook screenshot

Share:
14,470
Moshe
Author by

Moshe

iOS Engineer Website | GitHub | Careers | App Store

Updated on June 05, 2022

Comments

  • Moshe
    Moshe almost 2 years

    I've seen all the talk on the web about the difficulty of using a custom font in iPhone OS. Is it possible, to load a font from within my bundle into the UIWebView component?

    EDIT: I saw it in the app called "Omer" by "Rusty Brick"... It seems to be a web view, because I see a delay and during the loading delay the custom area is white. What other component could this have been?

    EDIT2: I contacted Rusty Brick. They told me which font it was. A standard font.

  • Willster
    Willster about 13 years
    @Moshe Just an innocent example font ;)
  • sgress454
    sgress454 about 12 years
    This answer is only half-right. The other half is referenced in the answer below by @tscheepers--you need to add the font name to your .plist file. AND one final thing. If you want to use a variant of a font (say DroidSerif Italic) then you need to do two things: first, add the DroidSerif Italic.ttf font to the bundle and the .plist, then, when you reference it in your stylesheet, enter the font-family as "DroidSerif-Italic" (note the dash instead of space).
  • sgress454
    sgress454 about 12 years
    This answer is half-right: see my comment above on @Willster's post.
  • Lukasz
    Lukasz over 11 years
    This answer worked for me on: iOS 6.0, iOS 5.1. Not tested on iOS 4.x
  • Boda
    Boda over 9 years
    @sgress454 Even though DroidSerif-Italic doesn't represent a family name, it represents a font name inside the family, but it did the trick, you are a saver ;)
  • Fattie
    Fattie almost 7 years
    Indeed this answer is not, really, correct. Answer below is correct.