Skip to content

Instantly share code, notes, and snippets.

@paiv
Created December 17, 2012 11:46
Show Gist options
  • Save paiv/4317685 to your computer and use it in GitHub Desktop.
Save paiv/4317685 to your computer and use it in GitHub Desktop.
iOS Fonts table view
//
// FontsTableViewController.m
//
// Created by Pavel Ivashkov on 3/30/11.
//
@implementation FontsTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
if (!(self = [super initWithStyle:style]))
return nil;
self.sampleText = @"The quick brown fox jumps over the lazy dog.";
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSArray *list = [UIFont familyNames];
return [list count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *families = [UIFont familyNames];
NSString *family = [families objectAtIndex:section];
NSArray *list = [UIFont fontNamesForFamilyName:family];
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
NSArray *families = [UIFont familyNames];
NSString *family = [families objectAtIndex:indexPath.section];
NSArray *list = [UIFont fontNamesForFamilyName:family];
NSString *fontName = [list objectAtIndex:indexPath.row];
UIFont *font = [UIFont fontWithName:fontName size:14];
UILabel *label = [[[UILabel alloc] init] autorelease];
label.font = font;
label.text = self.sampleText;
[label sizeToFit];
CGRect rect = label.frame;
CGFloat offsetY = round((cell.bounds.size.height - rect.size.height) / 2);
rect = CGRectOffset(rect, 2, offsetY);
label.frame = rect;
[cell.contentView addSubview:label];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *families = [UIFont familyNames];
NSString *family = [families objectAtIndex:section];
return family;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment