« November 2008 | Main | January 2009 »

December 04, 2008

Enumerating and visualizing all fonts on iPhone

When you're designing custom views or otherwise theming your iPhone application, it's nice to be able to see all of the available fonts. I couldn't find anything online that showed them, so I made my own little application. You can get it at FontListViz.tgz.



Screenshot 2008.12.04 01.27.59.jpg



It's pretty well dirt simple. Get all font family names, then get all of the font names in each family and store them in an array, then put each font name's text in a table view cell and set the table view cell's font using -[UIFont fontWithName:size:] and, well, Bob's your uncle -- instant very simple font viewer.

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.fontNames = [NSMutableArray array];
    NSArray *fontFamilyNames = [UIFont familyNames];
    for (NSString *familyName in fontFamilyNames) {
        NSLog(@"familyName = %@", familyName);
        NSArray *names = [UIFont fontNamesForFamilyName:familyName];
        NSLog(@"FontNames = %@", fontNames);
        [self.fontNames addObjectsFromArray:names];
    }
}
.
.
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    NSString *name = [self.fontNames objectAtIndex:indexPath.row];
    cell.text = name;
    cell.font = [UIFont fontWithName:name size:14];
    return cell;
}

Microsoft Office Formats won't load on the iPhone simulator

Just an FYI:

The UIWebView in the simulator remains blank when loading MS office formats like word (.doc), excel (.xls), powerpoint (.ppt) (and possibly iWork formats as well -- Go Go Gadget Google indexer), but it does work on the device itself. Caveat coder. Don't pull your hair out for too long wondering why your files don't show up on the simulator. They will once you compile and install on the phone.
    NSString *filePath = @"pathToAnExcelDocument.xls";
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == YES) 
    {
        NSLog(@"Loading %@", filePath);
        NSURL *pathURL = [NSURL fileURLWithPath:filePath];
        //Warning: this doesn't load into the view on the simulator
        NSURLRequest *pathURLRequest = [NSURLRequest requestWithURL:pathURL];
        [someWebView loadRequest:pathURLRequest];
    }
radar://6417654