《精通 iOS 开发》第 09 章学习笔记

Font应用的基础框架

1.工厂方法代码示例:

1
2
3
4
5
6
7
8
9
10
+ (instancetype)sharedFavoritesList {
static BIDFavoritesList *shared = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});

return shared;
}

2.读取用户偏好设置代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (instancetype)init {
self = [super init];
if (self) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *storedFavorites = [defaults objectForKey:@"favorites"];

if (storedFavorites) {
self.favorites = [storedFavorites mutableCopy];
} else {
self.favorites = [NSMutableArray array];
}
}
return self;
}

3.保存用户偏好设置代码示例:

1
2
3
4
5
6
- (void)moveItemAtIndex:(NSInteger)from toIndex:(NSInteger)to {
id item = _favorites[from];
[_favorites removeObjectAtIndex:from];
[_favorites insertObject:item atIndex:to];
[self saveFavorites];
}

创建根视图控制器

1.获取所有已知字体名字:

1
2
3
4
5
6
7
8
9
10
- (void)viewDidLoad {
[super viewDidLoad];

self.familyNames = [[UIFont familyNames] sortedArrayUsingSelector:@selector(compare:)];

UIFont *preferredTableViewFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];

self.cellPointSize = preferredTableViewFont.pointSize;
self.favoritesList = [BIDFavoritesList sharedFavoritesList];
}

2.设置分区标题:

1
2
3
4
5
6
7
8
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"All Font Families";
} else {
return @"My Favorite Fonts";
}
}

3.根据字体大小设置每行高度:

1
2
3
4
5
6
7
8
9
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
UIFont *font = [self fontForDisplayAtIndexPath:indexPath];
return 25 + font.ascender - font.descender;
} else {
return tableView.rowHeight;
}
}

创建字体列表视图控制器

1.添加编辑按钮:

1
2
3
4
5
6
7
8
9
10
11
12
- (void)viewDidLoad {
[super viewDidLoad];

UIFont *preferredTableViewFont =
[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];

self.cellPointSize = preferredTableViewFont.pointSize;

if (self.showsFavorites) {
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
}

2.在视图将要出现时,重载数据:

1
2
3
4
5
6
- (void)viewWillAppear:(BOOL)animated {
if (self.showsFavorites) {
self.fontNames = [BIDFavoritesList sharedFavoritesList].favorites;
[self.tableView reloadData];
}
}

3.使每行数据都能编辑:

1
2
3
4
5
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return self.showsFavorites;
}

4.删除某行数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.showsFavorites) {
return;
}
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *favorite = self.fontNames[indexPath.row];
[[BIDFavoritesList sharedFavoritesList] removeFavorite:favorite];
self.fontNames = [BIDFavoritesList sharedFavoritesList].favorites;

// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[ 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
}
}

5.调整某行数据:

1
2
3
4
5
6
7
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
[[BIDFavoritesList sharedFavoritesList] moveItemAtIndex:fromIndexPath.row
toIndex:toIndexPath.row];
self.fontNames = [BIDFavoritesList sharedFavoritesList].favorites;
}

6.场景跳转时的数据处理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
UIFont *font = [self fontForDisplayAtIndexPath:indexPath];

[segue.destinationViewController navigationItem].title = font.fontName;

if ([segue.identifier isEqualToString:@"ShowFontSizes"]) {
BIDFontSizesViewController *sizeVC = segue.destinationViewController;
sizeVC.font = font;
} else {
BIDFontInfoViewController *infoVC = segue.destinationViewController;
infoVC.font = font;
infoVC.favorite = [[BIDFavoritesList sharedFavoritesList]
.favorites containsObject:font.fontName];
}
}

创建字体尺寸视图控制器

1.字体大小数组初始化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- (NSArray *)pointSizes {
static NSArray *pointSizes = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
pointSizes = @[
@9,
@10,
@11,
@12,
@13,
@14,
@18,
@24,
@36,
@48,
@64,
@72,
@96,
@144
];
});
return pointSizes;
}