forked from mobilehub/CoreTextToy
/
CCoreTextLabel.m
322 lines (270 loc) · 11.9 KB
/
CCoreTextLabel.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
//
// CCoreTextLabel.m
// TouchCode
//
// Created by Jonathan Wight on 07/12/11.
// Copyright 2011 toxicsoftware.com. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY TOXICSOFTWARE.COM ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOXICSOFTWARE.COM OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of toxicsoftware.com.
#import "CCoreTextLabel.h"
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
#import "CMarkupValueTransformer.h"
#import "CCoreTextRenderer.h"
#import "UIFont_CoreTextExtensions.h"
#import "UIColor+Hex.h"
@interface CCoreTextLabel ()
@property (readwrite, nonatomic, retain) CCoreTextRenderer *renderer;
@property (readwrite, nonatomic, retain) UITapGestureRecognizer *tapRecognizer;
+ (NSAttributedString *)normalizeString:(NSAttributedString *)inString font:(UIFont *)inBaseFont textColor:(UIColor *)inTextColor alignment:(UITextAlignment)inTextAlignment lineBreakMode:(UILineBreakMode)inLineBreakMode;
- (void)tap:(UITapGestureRecognizer *)inGestureRecognizer;
@end
@implementation CCoreTextLabel
@synthesize text;
@synthesize insets;
@synthesize URLHandler;
@synthesize font;
@synthesize textColor;
@synthesize textAlignment;
@synthesize lineBreakMode;
@synthesize renderer;
@synthesize tapRecognizer;
+ (CGSize)sizeForString:(NSAttributedString *)inString font:(UIFont *)inBaseFont textColor:(UIColor *)inTextColor alignment:(UITextAlignment)inTextAlignment lineBreakMode:(UILineBreakMode)inLineBreakMode thatFits:(CGSize)inSize
{
NSAttributedString *theNormalizedText = [self normalizeString:inString font:inBaseFont textColor:inTextColor alignment:inTextAlignment lineBreakMode:inLineBreakMode];
CGSize theSize = [CCoreTextRenderer sizeForString:theNormalizedText thatFits:inSize];
return(theSize);
}
+ (NSAttributedString *)normalizeString:(NSAttributedString *)inString font:(UIFont *)inBaseFont textColor:(UIColor *)inTextColor alignment:(UITextAlignment)inTextAlignment lineBreakMode:(UILineBreakMode)inLineBreakMode
{
NSMutableAttributedString *theMutableText = [[CMarkupValueTransformer normalizedAttributedStringForAttributedString:inString baseFont:inBaseFont] mutableCopy];
UIFont *theFont = inBaseFont ?: [UIFont systemFontOfSize:17.0];
UIColor *theColor = inTextColor ?: [UIColor blackColor];
[theMutableText enumerateAttributesInRange:(NSRange){ .length = theMutableText.length } options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
if ([attrs objectForKey:(__bridge NSString *)kCTFontAttributeName] == NULL)
{
[theMutableText addAttribute:(__bridge NSString *)kCTFontAttributeName value:(__bridge id)theFont.CTFont range:range];
}
if ([attrs objectForKey:(__bridge NSString *)kCTForegroundColorAttributeName] == NULL)
{
[theMutableText addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:(__bridge id)theColor.CGColor range:range];
}
// [DW]
if ([attrs objectForKey:kMarkupTextColorAttributeName] != NULL)
{
NSString *theColorStr = [attrs objectForKey:kMarkupTextColorAttributeName];
if(theColorStr)
[theMutableText addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName value:(__bridge id)[UIColor colorWithHexString:theColorStr].CGColor range:range];
}
}];
CTTextAlignment theTextAlignment;
switch (inTextAlignment)
{
case UITextAlignmentLeft:
theTextAlignment = kCTLeftTextAlignment;
break;
case UITextAlignmentCenter:
theTextAlignment = kCTCenterTextAlignment;
break;
case UITextAlignmentRight:
theTextAlignment = kCTRightTextAlignment;
break;
}
// UILineBreakMode maps 1:1 to CTLineBreakMode
CTLineBreakMode theLineBreakMode = inLineBreakMode;
CTParagraphStyleSetting theSettings[] = {
{ .spec = kCTParagraphStyleSpecifierAlignment, .valueSize = sizeof(theTextAlignment), .value = &theTextAlignment, },
{ .spec = kCTParagraphStyleSpecifierLineBreakMode, .valueSize = sizeof(theLineBreakMode), .value = &theLineBreakMode, },
};
CTParagraphStyleRef theParagraphStyle = CTParagraphStyleCreate( theSettings, 2 );
NSDictionary *theAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer id)theParagraphStyle, (__bridge id)kCTParagraphStyleAttributeName,
NULL];
[theMutableText addAttributes:theAttributes range:(NSRange){ .length = [theMutableText length] }];
return(theMutableText);
}
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]) != NULL)
{
self.contentMode = UIViewContentModeRedraw;
font = [UIFont systemFontOfSize:17];
textColor = [UIColor blackColor];
textAlignment = UITextAlignmentLeft;
lineBreakMode = UILineBreakModeTailTruncation;
}
return(self);
}
- (id)initWithCoder:(NSCoder *)inCoder
{
if ((self = [super initWithCoder:inCoder]) != NULL)
{
self.contentMode = UIViewContentModeRedraw;
font = [UIFont systemFontOfSize:17];
textColor = [UIColor blackColor];
textAlignment = UITextAlignmentLeft;
lineBreakMode = UILineBreakModeTailTruncation;
}
return(self);
}
#pragma mark -
- (void)setFrame:(CGRect)inFrame
{
[super setFrame:inFrame];
self.renderer = NULL;
[self setNeedsDisplay];
}
#pragma mark -
- (void)setTextAlignment:(UITextAlignment)inTextAlignment
{
if (textAlignment != inTextAlignment)
{
textAlignment = inTextAlignment;
self.renderer = NULL;
[self setNeedsDisplay];
}
}
- (void)setLineBreakMode:(UILineBreakMode)inLineBreakMode
{
if (lineBreakMode != inLineBreakMode)
{
lineBreakMode = inLineBreakMode;
self.renderer = NULL;
[self setNeedsDisplay];
}
}
- (void)setText:(NSAttributedString *)inText
{
if (text != inText)
{
text = inText;
self.renderer = NULL;
[self setNeedsDisplay];
}
}
- (void)setInsets:(UIEdgeInsets)inInsets
{
insets = inInsets;
self.renderer = NULL;
[self setNeedsDisplay];
}
- (void)setURLHandler:(void (^)(NSURL *))inURLHandler
{
if (URLHandler != inURLHandler)
{
URLHandler = [inURLHandler copy];
//
if (URLHandler != NULL)
{
self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self addGestureRecognizer:self.tapRecognizer];
}
else
{
[self removeGestureRecognizer:self.tapRecognizer];
self.tapRecognizer = NULL;
}
}
}
#pragma mark -
- (CCoreTextRenderer *)renderer
{
if (renderer == NULL)
{
NSAttributedString *theNormalizedText = [[self class] normalizeString:self.text font:self.font textColor:self.textColor alignment:self.textAlignment lineBreakMode:self.lineBreakMode];
CGRect theBounds = self.bounds;
theBounds = UIEdgeInsetsInsetRect(theBounds, self.insets);
renderer = [[CCoreTextRenderer alloc] initWithText:theNormalizedText size:theBounds.size];
#warning TODO make constants for backgroundColor and strikeColor
[renderer addPrerendererBlock:^(CGContextRef inContext, CTRunRef inRun, CGRect inRect) {
NSDictionary *theAttributes2 = (__bridge NSDictionary *)CTRunGetAttributes(inRun);
CGColorRef theColor2 = (__bridge CGColorRef)[theAttributes2 objectForKey:@"backgroundColor"];
CGContextSetFillColorWithColor(inContext, theColor2);
CGContextFillRect(inContext, inRect);
} forAttributeKey:@"backgroundColor"];
[renderer addPrerendererBlock:^(CGContextRef inContext, CTRunRef inRun, CGRect inRect) {
NSDictionary *theAttributes2 = (__bridge NSDictionary *)CTRunGetAttributes(inRun);
CGColorRef theColor2 = (__bridge CGColorRef)[theAttributes2 objectForKey:@"strikeColor"];
CGContextSetStrokeColorWithColor(inContext, theColor2);
CGContextMoveToPoint(inContext, CGRectGetMinX(inRect), CGRectGetMidY(inRect));
CGContextAddLineToPoint(inContext, CGRectGetMaxX(inRect), CGRectGetMidY(inRect));
CGContextStrokePath(inContext);
} forAttributeKey:@"strikeColor"];
}
return(renderer);
}
#pragma mark -
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize theSize = size;
theSize.width -= self.insets.left + self.insets.right;
theSize.height -= self.insets.top + self.insets.bottom;
theSize = [self.renderer sizeThatFits:theSize];
theSize.width += self.insets.left + self.insets.right;
theSize.height += self.insets.top + self.insets.bottom;
return(theSize);
}
- (void)drawRect:(CGRect)rect
{
// ### Work out the inset bounds...
CGRect theBounds = self.bounds;
theBounds = UIEdgeInsetsInsetRect(theBounds, self.insets);
// ### Get and set up the context...
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(theContext);
CGContextTranslateCTM(theContext, theBounds.origin.x, theBounds.origin.y);
[self.renderer drawInContext:theContext];
CGContextRestoreGState(theContext);
// If you wanted to dump the rendered images to disk so you can make sure it is rendering correctly here's how you could do it...
// #if 1
// UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
// theContext = UIGraphicsGetCurrentContext();
// [self.renderer drawInContext:theContext];
// UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// [UIImagePNGRepresentation(theImage) writeToFile:[NSString stringWithFormat:@"/Users/schwa/Desktop/%d.png", [theImage hash]] atomically:NO];
// #endif
}
- (void)tap:(UITapGestureRecognizer *)inGestureRecognizer
{
CGPoint theLocation = [inGestureRecognizer locationInView:self];
theLocation.x -= self.insets.left;
theLocation.y -= self.insets.top;
NSDictionary *theAttributes = [self.renderer attributesAtPoint:theLocation];
NSURL *theLink = [theAttributes objectForKey:kMarkupLinkAttributeName];
if (theLink != NULL)
{
if (self.URLHandler != NULL)
{
self.URLHandler(theLink);
}
}
}
#pragma mark -
- (NSArray *)rectsForRange:(NSRange)inRange;
{
return([self.renderer rectsForRange:inRange]);
}
@end