This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathJTextView.m
325 lines (271 loc) · 10.5 KB
/
JTextView.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// JTextView.m
// JKit
//
// Created by Jeremy Tregunna on 10-10-24.
// Copyright (c) 2010 Jeremy Tregunna. All rights reserved.
//
#import <CoreText/CoreText.h>
#import "JTextView.h"
static CGFloat const kJTextViewPaddingSize = 2.0f;
@interface JTextView (PrivateMethods)
- (void)dataDetectorPassInRange:(NSRange)range;
- (void)receivedTap:(UITapGestureRecognizer*)recognizer;
@end
@implementation JTextView
@synthesize attributedText = _textStore;
@synthesize font = _font;
@synthesize textColor = _textColor;
@synthesize editable = _editable;
@synthesize dataDetectorTypes = _dataDetectorTypes;
@synthesize dataDelegate = _dataDelegate;
#pragma mark -
#pragma mark Object creation and destruction
- (id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame]))
{
_textStore = [[NSMutableAttributedString alloc] init];
self.backgroundColor = [UIColor whiteColor];
_textColor = [UIColor blackColor];
_font = [[UIFont systemFontOfSize:[UIFont systemFontSize]] retain];
_editable = NO;
_dataDetectorTypes = UIDataDetectorTypeNone;
caret = [[JTextCaret alloc] initWithFrame:CGRectZero];
UITapGestureRecognizer* tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(receivedTap:)] autorelease];
[self addGestureRecognizer:tap];
}
return self;
}
- (void)dealloc
{
CFRelease(textFrame);
[_font release];
[caret release];
[_textStore release];
[super dealloc];
}
#pragma mark -
#pragma mark Responder chain
- (BOOL)canBecomeFirstResponder
{
return self.editable;
}
#pragma mark -
#pragma mark Text drawing
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
[self.backgroundColor set];
CGContextFillRect(context, rect);
NSRange textRange = NSMakeRange(0, self.attributedText.length);
CTFontRef font = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
[self.attributedText addAttribute:(NSString*)kCTFontAttributeName value:(id)font range:textRange];
CFRelease(font);
if(!self.editable)
[self dataDetectorPassInRange:textRange];
CGFloat width = CGRectGetWidth(self.bounds) - kJTextViewPaddingSize * 2;
CGSize textSize = [[self.attributedText string] sizeWithFont:self.font
constrainedToSize:CGSizeMake(width, UINT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
CGContextTranslateCTM(context, 0, textSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, 1.0));
UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(kJTextViewPaddingSize, -(kJTextViewPaddingSize - 1), width, textSize.height)];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attributedText);
textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path.CGPath, NULL);
CFRelease(framesetter);
CTFrameDraw(textFrame, context);
UIGraphicsPushContext(context);
}
#pragma mark -
#pragma mark UIKeyInput delegate methods
- (BOOL)hasText
{
if(self.attributedText.length > 0)
return YES;
return NO;
}
- (void)insertText:(NSString*)aString
{
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:aString];
[self.attributedText appendAttributedString:attributedString];
[attributedString release];
[self setNeedsDisplay];
}
- (void)deleteBackward
{
if(self.attributedText.length != 0)
{
NSRange range = NSMakeRange(self.attributedText.length - 1, 1);
[self.attributedText deleteCharactersInRange:range];
[self setNeedsDisplay];
}
}
#pragma mark -
#pragma mark Data detectors
- (void)dataDetectorPassInRange:(NSRange)range
{
if (!self.dataDetectorTypes) {
return;
}
NSError* error = NULL;
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:self.dataDetectorTypes error:&error];
NSAssert(error == nil, @"Problem creating the link detector: %@", [error localizedDescription]);
NSString* string = [self.attributedText string];
[detector enumerateMatchesInString:string options:0 range:range usingBlock:^(NSTextCheckingResult* match, NSMatchingFlags flags, BOOL* stop){
NSRange matchRange = [match range];
// No way to call into Calendar, so don't detect dates
if([match resultType] != NSTextCheckingTypeDate)
{
[self.attributedText addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:matchRange];
[self.attributedText addAttribute:(NSString*)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:matchRange];
}
switch([match resultType])
{
case NSTextCheckingTypeLink:
{
NSURL* url = [match URL];
[self.attributedText addAttribute:kJTextViewDataDetectorLinkKey value:url range:matchRange];
break;
}
case NSTextCheckingTypePhoneNumber:
{
NSString* phoneNumber = [match phoneNumber];
[self.attributedText addAttribute:kJTextViewDataDetectorPhoneNumberKey value:phoneNumber range:matchRange];
break;
}
case NSTextCheckingTypeAddress:
{
NSDictionary* addressComponents = [match addressComponents];
[self.attributedText addAttribute:kJTextViewDataDetectorAddressKey value:addressComponents range:matchRange];
break;
}
case NSTextCheckingTypeDate:
{
//NSDate* date = [match date];
//[self.attributedText addAttribute:kJTextViewDataDetectorDateKey value:date range:matchRange];
break;
}
}
}];
}
#pragma mark -
#pragma mark Touch handling
- (void)receivedTap:(UITapGestureRecognizer*)recognizer
{
if(self.editable)
{
[self becomeFirstResponder];
return;
}
CGPoint point = [recognizer locationInView:self];
CGContextRef context = UIGraphicsGetCurrentContext();
NSArray* tempLines = (NSArray*)CTFrameGetLines(textFrame);
CFIndex lineCount = [tempLines count];//CFArrayGetCount(lines);
NSMutableArray* lines = [NSMutableArray arrayWithCapacity:lineCount];
for(id elem in [tempLines reverseObjectEnumerator])
[lines addObject:elem];
CGPoint origins[lineCount];
CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins);
for(CFIndex idx = 0; idx < lineCount; idx++)
{
CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, idx);
CGRect lineBounds = CTLineGetImageBounds(line, context);
lineBounds.origin.y += origins[idx].y;
if(CGRectContainsPoint(lineBounds, point))
{
CFArrayRef runs = CTLineGetGlyphRuns(line);
for(CFIndex j = 0; j < CFArrayGetCount(runs); j++)
{
CTRunRef run = CFArrayGetValueAtIndex(runs, j);
NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);
BOOL result = NO;
NSURL* url = [attributes objectForKey:kJTextViewDataDetectorLinkKey];
NSString* phoneNumber = [attributes objectForKey:kJTextViewDataDetectorPhoneNumberKey];
NSDictionary* addressComponents = [attributes objectForKey:kJTextViewDataDetectorAddressKey];
NSDate* date = [attributes objectForKey:kJTextViewDataDetectorDateKey];
if(url)
{
if ([_dataDelegate respondsToSelector:@selector(linkClicked:ofType:)]) {
[_dataDelegate linkClicked:url ofType:kJTextViewDataDetectorLinkKey];
} else {
result = [[UIApplication sharedApplication] openURL:url];
}
return;
}
else if(phoneNumber)
{
if ([_dataDelegate respondsToSelector:@selector(linkClicked:ofType:)]) {
[_dataDelegate linkClicked:phoneNumber ofType:kJTextViewDataDetectorPhoneNumberKey];
} else {
phoneNumber = [phoneNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// The following code may be switched to if we absolutely need to remove everything but the numbers.
//NSMutableString* strippedPhoneNumber = [NSMutableString stringWithCapacity:[phoneNumber length]]; // Can't be longer than that
//for(NSUInteger i = 0; i < [phoneNumber length]; i++)
//{
// if(isdigit([phoneNumber characterAtIndex:i]))
// [strippedPhoneNumber appendFormat:@"%c", [phoneNumber characterAtIndex:i]];
//}
//NSLog(@"*** phoneNumber = %@; strippedPhoneNumber = %@", phoneNumber, strippedPhoneNumber);
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phoneNumber]];
result = [[UIApplication sharedApplication] openURL:url];
}
return;
}
else if(addressComponents)
{
if ([_dataDelegate respondsToSelector:@selector(linkClicked:ofType:)]) {
[_dataDelegate linkClicked:addressComponents ofType:kJTextViewDataDetectorAddressKey];
} else {
NSMutableString* address = [NSMutableString string];
NSString* temp = nil;
if((temp = [addressComponents objectForKey:NSTextCheckingStreetKey]))
[address appendString:temp];
if((temp = [addressComponents objectForKey:NSTextCheckingCityKey]))
[address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
if((temp = [addressComponents objectForKey:NSTextCheckingStateKey]))
[address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
if((temp = [addressComponents objectForKey:NSTextCheckingZIPKey]))
[address appendString:[NSString stringWithFormat:@" %@", temp]];
if((temp = [addressComponents objectForKey:NSTextCheckingCountryKey]))
[address appendString:[NSString stringWithFormat:@"%@%@", ([address length] > 0) ? @", " : @"", temp]];
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
result = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
return;
} else if(date)
{
if ([_dataDelegate respondsToSelector:@selector(linkClicked:ofType:)]) {
[_dataDelegate linkClicked:date ofType:kJTextViewDataDetectorDateKey];
} else {
NSLog(@"Unable to handle date: %@", date);
result = NO;
}
return;
}
}
}
}
}
- (void)setText:(NSString*) newText;
{
NSAttributedString *attribString = [[NSAttributedString alloc] initWithString:newText];
[_textStore setAttributedString:attribString];
[attribString release];
}
- (NSString*)text;
{
return [_textStore string];
}
+ (CGFloat)suggestedHeightOfText:(NSString*)text forWidth:(float)maxWidth inFont:(UIFont*)font;
{
// Accomodate for padding
CGFloat width = maxWidth - kJTextViewPaddingSize * 2;
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(width, UINT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
@end