forked from sprang/Inkpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWDFontLibraryController.m
More file actions
154 lines (116 loc) · 5.23 KB
/
WDFontLibraryController.m
File metadata and controls
154 lines (116 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// WDFontLibraryController.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2010-2013 Steve Sprang
//
#import "WDCoreTextLabel.h"
#import "WDFontLibraryController.h"
#import "WDFontManager.h"
#define kCoreTextLabelWidth 300
#define kCoreTextLabelHeight 43
#define kCoreTextLabelTag 1
@implementation WDFontLibraryController
@synthesize table;
@synthesize selectedFonts;
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) {
return nil;
}
UIBarButtonItem *trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self action:@selector(deleteSelectedFonts:)];
self.navigationItem.rightBarButtonItem = trashItem;
trashItem.enabled = NO;
self.navigationItem.prompt = NSLocalizedString(@"Import your own fonts via Dropbox",
@"Import your own fonts via Dropbox");
self.selectedFonts = [NSMutableSet set];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fontAdded:) name:WDFontAddedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fontDeleted:) name:WDFontDeletedNotification object:nil];
self.title = NSLocalizedString(@"Font Library", @"Font Library");
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) properlyEnableTrashButton
{
self.navigationItem.rightBarButtonItem.enabled = (selectedFonts.count > 0 ? YES : NO);
}
- (void) loadView
{
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 428) style:UITableViewStylePlain];
self.view = table;
table.delegate = self;
table.dataSource = self;
table.allowsSelection = YES;
self.preferredContentSize = table.frame.size;
}
- (void) fontAdded:(NSNotification *)aNotification
{
NSString *fontName = (aNotification.userInfo)[@"name"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[[[WDFontManager sharedInstance] userFonts] indexOfObject:fontName] inSection:0];
[table beginUpdates];
[table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[table endUpdates];
}
- (void) fontDeleted:(NSNotification *)aNotification
{
NSNumber *index = (aNotification.userInfo)[@"index"];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index.integerValue inSection:0];
[table beginUpdates];
[table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[table endUpdates];
[self properlyEnableTrashButton];
}
- (void) deleteSelectedFonts:(id)sender
{
for (NSString *fontName in selectedFonts) {
[[WDFontManager sharedInstance] deleteUserFontWithName:fontName];
}
[selectedFonts removeAllObjects];
[self properlyEnableTrashButton];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [[[WDFontManager sharedInstance] userFonts] count];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *fontName = [[WDFontManager sharedInstance] userFonts][indexPath.row];
if ([selectedFonts containsObject:fontName]) {
[selectedFonts removeObject:fontName];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
} else {
[selectedFonts addObject:fontName];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
[self properlyEnableTrashButton];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"fontIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
WDCoreTextLabel *label = [[WDCoreTextLabel alloc] initWithFrame:CGRectMake(10, 0, kCoreTextLabelWidth - 10, kCoreTextLabelHeight)];
label.tag = kCoreTextLabelTag;
[cell.contentView addSubview:label];
}
NSString *fontName = [[WDFontManager sharedInstance] userFonts][indexPath.row];
WDCoreTextLabel *label = (WDCoreTextLabel *) [cell viewWithTag:kCoreTextLabelTag];
CTFontRef fontRef = [[WDFontManager sharedInstance] newFontRefForFont:fontName withSize:22];
[label setFontRef:fontRef];
CFRelease(fontRef);
[label setText:[[WDFontManager sharedInstance] displayNameForFont:fontName]];
cell.accessoryType = [selectedFonts containsObject:fontName] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
@end