Web development is one of those necessary evils in life. No-one enjoys working out what the hell is going on with Internet Explorer, and why your perfectly valid markup displays something like a spilt tin of alphabeti-spaghetti when viewed on what must be one of the worst bits of software known to man. It’s sad that with all the advances we’re seeing in HTML5 and CSS3 opening up the web to a whole new world of possibilities, Microsoft is stunting innovation.
When you start to deal with content that will only be viewed in your app, though, you can completely change your way of thinking. It’s surprising how years of keeping cross browser compatibility as a primary concern can be ingrained in your mind, but targeting just one browser and knowing that’s all you need to deal with is incredibly liberating. And kind of fun. Webkit, which is the underlying rendering engine for Safari and UIWebViews, is certainly one of the more advanced HTML engines, so you can play with CSS animations, 3D elements, HTML5 videos and, what we’re looking at today – custom fonts.
Thats right, we can finally use custom fonts without the proprietary crap that Microsoft implements. There are plenty of tutorials on how to use CSS3 fonts, but when dealing with UIWebViews, there are a few caveats you need to be aware of.
Are you local?
If you want your HTML and CSS files to live on a remote server, then it’s simply a case of doing something like the following to load your HTML:
NSURL *rizerURL = [NSURL URLWithString:@"http://www.rizergames.com"];
[webView loadRequest:[NSURLRequest requestWithURL:rizerURL];
You then reference your remote CSS files in the usual way. Very simple. I should note that referencing local CSS from within a remote HTML file is not possible. If you want to use HTML and CSS from within your application’s bundle, then you need to do the following:
NSString *htmlIndex = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[[self webView] loadHTMLString:htmlIndex baseURL:baseURL];
And then in your HTML you can simply use:
<link rel="stylesheet" type="text/css" href="default.css" />
Obviously both files need to be imported into your project like any other file. The key thing here is that you set the base URL so that the UIWebView knows where to look for the CSS. You could use the loadRequest method to load a local URL, but then you have no control over the base URL. I should also point out that not using loadRequest breaks all of the forwards and backwards navigation functionality of the UIWebView, which means you’ve got to roll your own (please someone tell me I’m wrong here, but after many hours of Googling, it looks like this is the way it is).
The Squirrel is your friend
So now you just look up the CSS3 syntax and plug in your TTF or OTF file, right? Wrong. Sadly these formats aren’t supported on iOS yet, so you have to use SVG. If you don’t have an SVG you can use Font Squirrel’s excellent conversion tool to create a very handy @font-face Kit. Check out their decent selection of free for commercial use fonts too, which all have @font-face Kits that you can download straight off. You can pick out the key bit of CSS from the files in the kit – it should look something like this:
@font-face
{
font-family: "Chunk Five";
src: url("Chunkfive-webfont.svg#webfontb5K2fJwj") format("svg");
font-weight: normal;
font-style: normal;
}
Import the SVG into your XCode project, then you can then just use these defined fonts in the normal way for your elements in your CSS:
h1
{
font-family: "Chunk Five", Helvetica;
}
One more thing to watch out for – the letter-spacing property doesn’t work with SVG fonts, so won’t work here. That should be all you need to use just about any licensed font you like. Still struggling? Post a comment and I’ll do my best to get back to you!