-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTHighlightView.m
275 lines (199 loc) · 11.1 KB
/
CTHighlightView.m
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// CTHighlightView.m
// iOS Syntax Highlighter
//
// Created by Andrew Boos on 4/3/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "CTHighlightView.h"
@interface CTHighlightViewDelegate : NSObject <UITextFieldDelegate>
@end
@implementation CTHighlightViewDelegate
//@synthesize scrollOffset;
- (void)textViewDidChange:(UITextView *)textView {
[textView setNeedsDisplay];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSString* newText = [text stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
if (![newText isEqualToString:text]) {
textView.text = [textView.text stringByReplacingCharactersInRange:range withString:newText];
return NO;
}
return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[scrollView setNeedsDisplay];
}
@end
static CGFloat MARGIN = 8;
@implementation CTHighlightView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.text = @"";
// self.backgroundColor = [UIColor whiteColor];
self.textColor = [UIColor clearColor];
self.textColor = [UIColor redColor];
internalDelegate = [[CTHighlightViewDelegate alloc] init];
self.delegate = internalDelegate;
self.text = [self.text stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
if (self.text.length > 0) {
//Prepare View for drawing
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, ([self bounds]).size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Get the view frame size
CGSize size = self.frame.size;
NSLog(@"CO:%f",self.contentOffset.y);
// Make a frame that has margins
CGRect workingFrame = CGRectMake(MARGIN + 0.0, (-self.contentOffset.y + 0), (size.width - 2*MARGIN ) , (size.height + self.contentOffset.y - MARGIN ));
//Set Font
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)self.font.fontName,
self.font.pointSize,
NULL);
//Set color
CGColorRef color = [UIColor blackColor].CGColor;
//CGFloat lineSpacing = 0.0;
//CGFloat marginTop = 0.0;
// CGFloat marginBot = 0.0;
CGFloat minlineHeight = [self.text sizeWithFont:self.font].height;
CGFloat maxlineHeight = [self.text sizeWithFont:self.font].height;
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
NSLog(@"MiLH:%f",minlineHeight);
NSLog(@"MzLH:%f",maxlineHeight);
CTParagraphStyleSetting setting[3] = {
{ kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(minlineHeight), &minlineHeight },
{ kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(maxlineHeight), &maxlineHeight },
//{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(marginTop), &marginTop},
//{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(marginBot), &marginBot},
//{ kCTParagraphStyleSpecifierLineSpacing, sizeof(lineSpacing), &lineSpacing},
{kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(setting, 3);
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)font,(NSString*)kCTFontAttributeName,
(__bridge id)color, (NSString*)kCTForegroundColorAttributeName,
(__bridge id)paragraphStyle, (NSString*)kCTParagraphStyleAttributeName,
nil];
//Create Attributed String
NSAttributedString* stringToDraw = [[NSAttributedString alloc]
initWithString:self.text
attributes:attributesDict];
stringToDraw = [self highlightText:stringToDraw];
// Create path
CGMutablePathRef gpath = CGPathCreateMutable();
CGPathAddRect(gpath, NULL, workingFrame);
CFAttributedStringRef attrString = (__bridge CFAttributedStringRef) stringToDraw;
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CTFrameRef theFrame =
CTFramesetterCreateFrame(framesetter, CFRangeMake(0,
CFAttributedStringGetLength(attrString)), gpath, NULL);
CTFrameDraw(theFrame, context);
} else {
self.text = [[NSString alloc] initWithString:@""];
}
}
- (NSRange)visibleRangeOfTextView:(UITextView *)textView {
CGRect bounds = textView.bounds;
UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end;
return NSMakeRange([textView offsetFromPosition:textView.beginningOfDocument toPosition:start],
[textView offsetFromPosition:start toPosition:end]);
}
- (NSAttributedString*)highlightText:(NSAttributedString *)stringIn {
NSMutableAttributedString* coloredString = [[NSMutableAttributedString alloc] initWithAttributedString:stringIn];
NSString* coloredStringPlain = stringIn.string;
//NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"objectivec" ofType:@"plist"];
//NSDictionary* dwarves = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSScanner *theScanner = [NSScanner scannerWithString:coloredStringPlain];
NSRange highlightRange = NSMakeRange (0,0);
NSUInteger lastLocation = 0;
//Digits
NSRegularExpression *squeezeNewlines = [NSRegularExpression regularExpressionWithPattern:@"\\d" options:0 error:nil];
NSArray* matches = [squeezeNewlines matchesInString:coloredStringPlain options:0 range:[coloredStringPlain rangeOfString:coloredStringPlain]];
NSLog(@"Matches:%@",matches);
for (NSTextCheckingResult *textMatch in matches) {
NSRange textMatchRange = [textMatch rangeAtIndex:0];
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor blueColor] CGColor]
range:textMatchRange];
}
//Add keyword highlighting
//Add ClassName highlighting
//Add ClassName highlighting
//Color Constants
//#import
squeezeNewlines = [NSRegularExpression regularExpressionWithPattern:@"#import" options:0 error:nil];
matches = [squeezeNewlines matchesInString:coloredStringPlain options:0 range:[coloredStringPlain rangeOfString:coloredStringPlain]];
NSLog(@"Matches:%@",matches);
for (NSTextCheckingResult *textMatch in matches) {
NSRange textMatchRange = [textMatch rangeAtIndex:0];
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor brownColor] CGColor]
range:textMatchRange];
}
//secondString
squeezeNewlines = [NSRegularExpression regularExpressionWithPattern:@"(')(.*?)(?<!\\\\)(\\1)|(')(.*?)(?<!\\\\)(\\r)|(')(.*?)(?<!\\\\)(\\n)|(')(.*?)(?<!\\\\)($)" options:0 error:nil];
matches = [squeezeNewlines matchesInString:coloredStringPlain options:0 range:[coloredStringPlain rangeOfString:coloredStringPlain]];
NSLog(@"Matches:%@",matches);
for (NSTextCheckingResult *textMatch in matches) {
NSRange textMatchRange = [textMatch rangeAtIndex:0];
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor blueColor] CGColor]
range:textMatchRange];
}
//first String
squeezeNewlines = [NSRegularExpression regularExpressionWithPattern:@"(\")(.*?)(?<!\\\\)(\\1)|(\")(.*?)(?<!\\\\)(\\r)|(\")(.*?)(?<!\\\\)(\\n)|(\")(.*?)(?<!\\\\)($)/s" options:0 error:nil];
matches = [squeezeNewlines matchesInString:coloredStringPlain options:0 range:[coloredStringPlain rangeOfString:coloredStringPlain]];
NSLog(@"Matches:%@",matches);
for (NSTextCheckingResult *textMatch in matches) {
NSRange textMatchRange = [textMatch rangeAtIndex:0];
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor redColor] CGColor]
range:textMatchRange];
}
//Single Line Comments
[theScanner setScanLocation:0];
while ([theScanner isAtEnd] == NO) {
lastLocation = theScanner.scanLocation;
[theScanner scanUpToString:@"//" intoString:nil];
NSUInteger beginHighlight = [theScanner scanLocation];
[theScanner scanUpToString:@"\n" intoString:nil];
NSUInteger endHighlight = [theScanner scanLocation];
highlightRange = NSMakeRange (beginHighlight, (endHighlight - beginHighlight));
if (highlightRange.length > 0) {
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor greenColor] CGColor]
range:highlightRange];
}
if (theScanner.scanLocation == lastLocation) {
break;
} }
//MultilineComment(begins with /* ends wit */)
squeezeNewlines = [NSRegularExpression regularExpressionWithPattern:@"\\/\\*[\\s\\S]*?\\*\\/|\\/\\*[\\s\\S]*?$" options:0 error:nil];
matches = [squeezeNewlines matchesInString:coloredStringPlain options:0 range:[coloredStringPlain rangeOfString:coloredStringPlain]];
NSLog(@"Matches:%@",matches);
for (NSTextCheckingResult *textMatch in matches) {
NSRange textMatchRange = [textMatch rangeAtIndex:0];
[coloredString addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor greenColor] CGColor]
range:textMatchRange];
}
NSAttributedString* coloredStringOut = coloredString.copy;
return coloredStringOut;
}
+ (CGFloat)lineHeight:(CTFontRef)font {
CGFloat ascent = CTFontGetAscent(font);
CGFloat descent = CTFontGetDescent(font);
CGFloat leading = CTFontGetLeading(font);
return ceilf(ascent + descent + leading);
}
@end