Operating systems by Apple Inc. have always enjoyed a good reputation for their excellent typography. Steve Jobs even told once about his passion for calligraphy: "...None of this had even a hope of any practical application in my life. But ten years later, when we were designing the first Macintosh computer, it all came back to me. And we designed it all into the Mac. It was the first computer with beautiful typography. If I had never dropped in on that single course in college, the Mac would have never had multiple typefaces or proportionally spaced fonts. And since Windows just copied the Mac, it's likely that no personal computer would have them. If I had never dropped out, I would have never dropped in on this calligraphy class, and personal computers might not have the wonderful typography that they do. Of course it was impossible to connect the dots looking forward when I was in college. But it was very, very clear looking backwards ten years later".
iOS, too, can be praised for its good fonts. Call me a Captain Obvious, but I want to remind you that in iOS you can use both default and custom fonts - you may require the latter if they fit better to overall application design and style. Then, I want to tell you something less obvious: you can load and change fonts in your app even when it's running! Now I'll tell you how.
Dynamic addition of fonts was amongst the most interesting problems our team encountered in one of our recently completed projects. We needed to give users a possibility to install their own fonts, since not all fonts included in iOS by default were demanded, and we wanted to provide users with more options to choose from.
We managed to address this with the help of two things: Dropbox Chooser and developers' skilled hands.
The task were divided into the following sub-tasks:
Getting a font and saving it into the application directory.
Installing the font.
Getting a font
It was implemented with the help of a new SDK from Dropbox team called DBChooser. This SDK helps to quickly receive URLs for files stored under users' Dropbox accounts.
Check-up and installation
After having saved a font in the app directory and having got a URL for a local file, a check-up should be performed to find out if the font can be installed and used in the app. The following checkup procedure can be used:
NSError *error;
NSData *inData = [NSData dataWithContentsOfURL:fontURL options:NSDataReadingMappedIfSafe error: &error];
if(inData) {
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (font != NULL) {
CFErrorRef error;
if (CTFontManagerRegisterGraphicsFont(font, &error)) {
//font can be used
//here you can save font name for quick use in future
NSString *fontName = (__bridge NSString *)CGFontCopyPostScriptName(font);
}
CFRelease(font);
}
CFRelease(provider);
}
In order to use the font at the next run of the application, it is necessary to install the font again when the application starts to load:
CFErrorRef error = NULL;
CFURLRef url = (__bridge CFURLRef)fontURL;
BOOL result = CTFontManagerRegisterFontsForURL(url, kCTFontManagerScopeNone, &error);
if (result) {
//font installed successfully
}
Now you're all set - you can add custom fonts in your app dynamically.
网友评论