Repository navigation
Expand file tree
/
Copy pathWDFontController.m
More file actions
206 lines (161 loc) · 7.27 KB
/
Copy pathWDFontController.m
File metadata and controls
206 lines (161 loc) · 7.27 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// WDTextController.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 <CoreText/CoreText.h>
#import "WDCanvasController.h"
#import "WDDrawingController.h"
#import "WDCoreTextLabel.h"
#import "WDFontController.h"
#import "WDFontManager.h"
#import "WDInspectableProperties.h"
#import "WDPropertyManager.h"
#define kMinFontSize 1
#define kMaxFontSize 200
#define kTableFadeRadius 6
#define kCoreTextLabelTag 1
@implementation WDFontController
@synthesize drawingController = drawingController_;
@synthesize selectedFontName = selectedFontName_;
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) setDrawingController:(WDDrawingController *)drawingController
{
drawingController_ = drawingController;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(invalidProperties:)
name:WDInvalidPropertiesNotification
object:drawingController.propertyManager];
}
- (IBAction) decrement:(id)sender
{
sizeSlider_.value = sizeSlider_.value - 1;
[sizeSlider_ sendActionsForControlEvents:UIControlEventTouchUpInside];
}
- (IBAction) increment:(id)sender
{
sizeSlider_.value = sizeSlider_.value + 1;
[sizeSlider_ sendActionsForControlEvents:UIControlEventTouchUpInside];
}
- (IBAction) takeFontSizeFrom:(id)sender
{
int size = round([(UISlider *)sender value]);
sizeLabel_.text = [NSString stringWithFormat:@"%d pt", size];
}
- (IBAction) takeFinalFontSizeFrom:(id)sender
{
int size = round([(UISlider *)sender value]);
sizeLabel_.text = [NSString stringWithFormat:@"%d pt", size];
[drawingController_ setValue:@(size) forProperty:WDFontSizeProperty];
}
- (IBAction) takeAlignmentFrom:(id)sender
{
[drawingController_ setValue:@(alignment_.selectedSegmentIndex) forProperty:WDTextAlignmentProperty];
}
- (void)viewDidLoad
{
[super viewDidLoad];
sizeSlider_.minimumValue = kMinFontSize;
sizeSlider_.maximumValue = kMaxFontSize;
alignment_.selectedSegmentIndex = [[drawingController_.propertyManager defaultValueForProperty:WDTextAlignmentProperty] intValue];
[alignment_ addTarget:self action:@selector(takeAlignmentFrom:) forControlEvents:UIControlEventValueChanged];
int size = [[drawingController_.propertyManager defaultValueForProperty:WDFontSizeProperty] intValue];
sizeSlider_.value = size;
sizeLabel_.text = [NSString stringWithFormat:@"%d pt", size];
self.title = NSLocalizedString(@"Font", @"Font");
self.preferredContentSize = self.view.frame.size;
}
- (void) scrollToSelectedFont
{
int fontIndex = [[[WDFontManager sharedInstance] supportedFonts] indexOfObject:[drawingController_.propertyManager defaultValueForProperty:WDFontNameProperty]];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:fontIndex inSection:0];
[table_ scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[table_ reloadData];
[self scrollToSelectedFont];
}
- (void) invalidProperties:(NSNotification *)aNotification
{
NSSet *properties = [aNotification userInfo][WDInvalidPropertiesKey];
for (NSString *property in properties) {
id value = [drawingController_.propertyManager defaultValueForProperty:property];
if ([property isEqualToString:WDFontNameProperty]) {
if (![value isEqualToString:self.selectedFontName]) {
[table_ reloadData];
[self scrollToSelectedFont];
}
} else if ([property isEqualToString:WDFontSizeProperty]) {
int size = [value intValue];
sizeSlider_.value = size;
sizeLabel_.text = [NSString stringWithFormat:@"%d pt", size];
} else if ([property isEqualToString:WDTextAlignmentProperty]) {
[alignment_ removeTarget:self action:@selector(takeAlignmentFrom:) forControlEvents:UIControlEventValueChanged];
alignment_.selectedSegmentIndex = [value intValue];
[alignment_ addTarget:self action:@selector(takeAlignmentFrom:) forControlEvents:UIControlEventValueChanged];
}
}
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [[[WDFontManager sharedInstance] supportedFonts] count];
}
- (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:CGRectInset(cell.contentView.bounds, 10, 0)];
label.tag = kCoreTextLabelTag;
[cell.contentView addSubview:label];
}
NSString *fontName = [[WDFontManager sharedInstance] supportedFonts][indexPath.row];
if ([fontName isEqualToString:[drawingController_.propertyManager defaultValueForProperty:WDFontNameProperty]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedFontName = fontName;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
WDCoreTextLabel *label = (WDCoreTextLabel *) [cell viewWithTag:kCoreTextLabelTag];
CTFontRef fontRef = [[WDFontManager sharedInstance] newFontRefForFont:fontName withSize:20];
[label setFontRef:fontRef];
CFRelease(fontRef);
[label setText:[[WDFontManager sharedInstance] displayNameForFont:fontName]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *oldCell = nil;
NSIndexPath *oldIndexPath = nil;
// find old cell
int oldRow = [[[WDFontManager sharedInstance] supportedFonts] indexOfObject:self.selectedFontName];
oldIndexPath = [NSIndexPath indexPathForRow:oldRow inSection:indexPath.section];
oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
self.selectedFontName = nil;
// deselect old cell
if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
// select new value
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedFontName = [[WDFontManager sharedInstance] supportedFonts][indexPath.row];
}
NSString *font = [[WDFontManager sharedInstance] supportedFonts][indexPath.row];
[drawingController_ setValue:font forProperty:WDFontNameProperty];
}
@end