Repository navigation
Expand file tree
/
Copy pathQEDTextView.m
More file actions
192 lines (166 loc) · 10.2 KB
/
Copy pathQEDTextView.m
File metadata and controls
192 lines (166 loc) · 10.2 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
//
// QEDTextView.m
// CYRTextViewExample
//
// Created by Illya Busigin on 1/10/14.
// Copyright (c) 2014 Cyrillian, Inc. All rights reserved.
//
#import "QEDTextView.h"
#import <CoreText/CoreText.h>
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]
@implementation QEDTextView
#pragma mark - Initialization & Setup
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self commonSetup];
}
return self;
}
- (void)commonSetup
{
_defaultFont = [UIFont systemFontOfSize:14.0f];
_boldFont = [UIFont boldSystemFontOfSize:14.0f];
_italicFont = [UIFont fontWithName:@"HelveticaNeue-Oblique" size:14.0f];
self.font = _defaultFont;
self.textColor = [UIColor blackColor];
[self addObserver:self forKeyPath:NSStringFromSelector(@selector(defaultFont)) options:NSKeyValueObservingOptionNew context:0];
[self addObserver:self forKeyPath:NSStringFromSelector(@selector(boldFont)) options:NSKeyValueObservingOptionNew context:0];
[self addObserver:self forKeyPath:NSStringFromSelector(@selector(italicFont)) options:NSKeyValueObservingOptionNew context:0];
if (_italicFont == nil && ([UIFontDescriptor class] != nil))
{
// This works around a bug in 7.0.3 where HelveticaNeue-Italic is not present as a UIFont option
_italicFont = (__bridge_transfer UIFont*)CTFontCreateWithName(CFSTR("HelveticaNeue-Italic"), 14.0f, NULL);
}
self.tokens = [self solverTokens];
}
- (NSArray *)solverTokens
{
NSArray *solverTokens = @[
[CYRToken tokenWithName:@"special_numbers"
expression:@"[ʝ]"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"mod"
expression:@"\bmod\b"
attributes:@{
NSForegroundColorAttributeName : RGB(245, 0, 110)
}],
[CYRToken tokenWithName:@"string"
expression:@"\".*?(\"|$)"
attributes:@{
NSForegroundColorAttributeName : RGB(24, 110, 109)
}],
[CYRToken tokenWithName:@"hex_1"
expression:@"\\$[\\d a-f]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"octal_1"
expression:@"&[0-7]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"binary_1"
expression:@"%[01]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"hex_2"
expression:@"0x[0-9 a-f]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"octal_2"
expression:@"0o[0-7]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"binary_2"
expression:@"0b[01]+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"float"
expression:@"\\d+\\.?\\d+e[\\+\\-]?\\d+|\\d+\\.\\d+|∞"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"integer"
expression:@"\\d+"
attributes:@{
NSForegroundColorAttributeName : RGB(0, 0, 255)
}],
[CYRToken tokenWithName:@"operator"
expression:@"[/\\*,\\;:=<>\\+\\-\\^!·≤≥]"
attributes:@{
NSForegroundColorAttributeName : RGB(245, 0, 110)
}],
[CYRToken tokenWithName:@"round_brackets"
expression:@"[\\(\\)]"
attributes:@{
NSForegroundColorAttributeName : RGB(161, 75, 0)
}],
[CYRToken tokenWithName:@"square_brackets"
expression:@"[\\[\\]]"
attributes:@{
NSForegroundColorAttributeName : RGB(105, 0, 0),
NSFontAttributeName : self.boldFont
}],
[CYRToken tokenWithName:@"absolute_brackets"
expression:@"[|]"
attributes:@{
NSForegroundColorAttributeName : RGB(104, 0, 111)
}],
[CYRToken tokenWithName:@"reserved_words"
expression:@"(abs|acos|acosh|asin|asinh|atan|atanh|atomicweight|ceil|complex|cos|cosh|crandom|deriv|erf|erfc|exp|eye|floor|frac|gamma|gaussel|getconst|imag|inf|integ|integhq|inv|ln|log10|log2|machineprecision|max|maximize|min|minimize|molecularweight|ncum|ones|pi|plot|random|real|round|sgn|sin|sqr|sinh|sqrt|tan|tanh|transpose|trunc|var|zeros)"
attributes:@{
NSForegroundColorAttributeName : RGB(104, 0, 111),
NSFontAttributeName : self.boldFont
}],
[CYRToken tokenWithName:@"chart_parameters"
expression:@"(chartheight|charttitle|chartwidth|color|seriesname|showlegend|showxmajorgrid|showxminorgrid|showymajorgrid|showyminorgrid|transparency|thickness|xautoscale|xaxisrange|xlabel|xlogscale|xrange|yautoscale|yaxisrange|ylabel|ylogscale|yrange)"
attributes:@{
NSForegroundColorAttributeName : RGB(11, 81, 195),
}],
[CYRToken tokenWithName:@"comment"
expression:@"//.*"
attributes:@{
NSForegroundColorAttributeName : RGB(31, 131, 0),
NSFontAttributeName : self.italicFont
}]
];
return solverTokens;
}
#pragma mark - Cleanup
- (void)dealloc
{
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(defaultFont))];
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(boldFont))];
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(italicFont))];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(defaultFont))] ||
[keyPath isEqualToString:NSStringFromSelector(@selector(boldFont))] ||
[keyPath isEqualToString:NSStringFromSelector(@selector(italicFont))])
{
// Reset the tokens, this will clear any existing formatting
self.tokens = [self solverTokens];
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - Overrides
- (void)setDefaultFont:(UIFont *)defaultFont
{
_defaultFont = defaultFont;
self.font = defaultFont;
}
@end