上一篇简单介绍了获取ios支持的字体样式的方法,并且给出了最终的结果,但直观效果不是很好,不能很好的感受到各种字体的不同。相信通过本篇可以弥补此缺憾,本篇通过uitableview以列表的形式显示各种字体样式。
//
// FontDemoViewController.h
// FontDemo
//
// Created by mac on 12-3-6.
// Copyright 2012年 __wwssttt@163.com__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FontDemoViewController : UITableViewController
@property(nonatomic,retain)NSArray* fontFamilies;
@property(nonatomic,retain)NSMutableArray* fontNames;
@end
//
// FontDemoViewController.m
// FontDemo
//
// Created by mac on 12-3-6.
// Copyright 2012年 __wwssttt@163.com__. All rights reserved.
//
#import "FontDemoViewController.h"
@implementation FontDemoViewController
@synthesize fontFamilies,fontNames;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [superinitWithStyle:style];
if (self) {
// Custom initialization
}
returnself;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[superviewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.fontNames = [[NSMutableArrayalloc] init];
self.fontFamilies = [UIFontfamilyNames];
for (NSString* families inself.fontFamilies) {
NSArray* tmp = [UIFontfontNamesForFamilyName:families];
[self.fontNamesaddObject:tmp];
}
}
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
if (self.fontFamilies != nil) {
[selfsetFontFamilies:nil];
}
if (self.fontNames != nil) {
[selfsetFontNames:nil];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[superviewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[superviewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return [self.fontFamiliescount];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
NSLog(@"%d",[[self.fontNamesobjectAtIndex:section] count]);
return [[self.fontNamesobjectAtIndex:section] count];
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.fontFamiliesobjectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];
}
int section = indexPath.section;
int row = indexPath.row;
NSLog(@"%@",[[self.fontNamesobjectAtIndex:section] objectAtIndex:row]);
cell.textLabel.text = [[self.fontNamesobjectAtIndex:section] objectAtIndex:row];
[cell.textLabelsetFont:[UIFontfontWithName:[[self.fontNamesobjectAtIndex:section] objectAtIndex:row] size:20]];
// Configure the cell...
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
@end
最终的结果如下: