-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTLabel.m
1085 lines (945 loc) · 40.7 KB
/
RTLabel.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// RTLabel.m
// RTLabelProject
//
/**
* Copyright (c) 2010 Muh Hon Cheng
* Created by honcheng on 1/6/11.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
* WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Muh Hon Cheng <[email protected]>
* @copyright 2011 Muh Hon Cheng
* @version
*
*/
#import "RTLabel.h"
@interface RTLabelButton : UIButton
@property (nonatomic, assign) int componentIndex;
@property (nonatomic) NSURL *url;
@end
@implementation RTLabelButton
@end
@implementation RTLabelComponent
- (id)initWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes
{
self = [super init];
if (self) {
_text = aText;
_tagLabel = aTagLabel;
_attributes = theAttributes;
}
return self;
}
+ (id)componentWithString:(NSString*)aText tag:(NSString*)aTagLabel attributes:(NSMutableDictionary*)theAttributes
{
return [[self alloc] initWithString:aText tag:aTagLabel attributes:theAttributes];
}
- (id)initWithTag:(NSString*)aTagLabel position:(int)aPosition attributes:(NSMutableDictionary*)theAttributes
{
self = [super init];
if (self) {
_tagLabel = aTagLabel;
_position = aPosition;
_attributes = theAttributes;
}
return self;
}
+(id)componentWithTag:(NSString*)aTagLabel position:(int)aPosition attributes:(NSMutableDictionary*)theAttributes
{
return [[self alloc] initWithTag:aTagLabel position:aPosition attributes:theAttributes];
}
- (NSString*)description
{
NSMutableString *desc = [NSMutableString string];
[desc appendFormat:@"text: %@", self.text];
[desc appendFormat:@", position: %i", self.position];
if (self.tagLabel) [desc appendFormat:@", tag: %@", self.tagLabel];
if (self.attributes) [desc appendFormat:@", attributes: %@", self.attributes];
return desc;
}
@end
@implementation RTLabelExtractedComponent
+ (RTLabelExtractedComponent*)rtLabelExtractComponentsWithTextComponent:(NSMutableArray*)textComponents plainText:(NSString*)plainText
{
RTLabelExtractedComponent *component = [[RTLabelExtractedComponent alloc] init];
[component setTextComponents:textComponents];
[component setPlainText:plainText];
return component;
}
@end
@interface RTLabel()
- (CGFloat)frameHeight:(CTFrameRef)frame;
- (NSArray *)components;
- (void)parse:(NSString *)data valid_tags:(NSArray *)valid_tags;
- (NSArray*) colorForHex:(NSString *)hexColor;
- (void)render;
#pragma mark -
#pragma mark styling
- (void)applyItalicStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyBoldStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyBoldItalicStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyColor:(NSString*)value toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applySingleUnderlineText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyDoubleUnderlineText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyUnderlineColor:(NSString*)value toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyFontAttributes:(NSDictionary*)attributes toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length;
- (void)applyParagraphStyleToText:(CFMutableAttributedStringRef)text attributes:(NSMutableDictionary*)attributes atPosition:(int)position withLength:(int)length;
@end
@implementation RTLabel
- (id)initWithFrame:(CGRect)_frame
{
self = [super initWithFrame:_frame];
if (self)
{
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self initialize];
}
return self;
}
- (void)initialize
{
[self setBackgroundColor:[UIColor clearColor]];
_font = [UIFont systemFontOfSize:15];
_textColor = [UIColor blackColor];
_text = @"";
_textAlignment = RTTextAlignmentLeft;
_lineBreakMode = RTTextLineBreakModeWordWrapping;
_lineSpacing = 3;
_currentSelectedButtonComponentIndex = -1;
_paragraphReplacement = @"\n";
[self setMultipleTouchEnabled:YES];
}
- (void)setTextAlignment:(RTTextAlignment)textAlignment
{
_textAlignment = textAlignment;
[self setNeedsDisplay];
}
- (void)setLineBreakMode:(RTTextLineBreakMode)lineBreakMode
{
_lineBreakMode = lineBreakMode;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[self render];
}
- (void)render
{
if (self.currentSelectedButtonComponentIndex==-1)
{
for (id view in [self subviews])
{
if ([view isKindOfClass:[UIView class]])
{
[view removeFromSuperview];
}
}
}
if (!self.plainText) return;
CGContextRef context = UIGraphicsGetCurrentContext();
if (context != NULL)
{
// Drawing code.
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,self.frame.size.height);
CGContextConcatCTM(context, flipVertical);
}
// Initialize an attributed string.
CFStringRef string = (__bridge CFStringRef)self.plainText;
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, (long)0);
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), string);
CFMutableDictionaryRef styleDict1 = ( CFDictionaryCreateMutable( (0), 0, (0), (0) ) );
// Create a color and add it as an attribute to the string.
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorSpaceRelease(rgbColorSpace);
CFDictionaryAddValue( styleDict1, kCTForegroundColorAttributeName, [self.textColor CGColor] );
CFAttributedStringSetAttributes( attrString, CFRangeMake( 0, CFAttributedStringGetLength(attrString) ), styleDict1, 0 );
CFMutableDictionaryRef styleDict = ( CFDictionaryCreateMutable( (0), (NSUInteger)0, (0), (0) ) );
[self applyParagraphStyleToText:attrString attributes:nil atPosition:0 withLength:(int)CFAttributedStringGetLength(attrString)];
CTFontRef thisFont = CTFontCreateWithName ((__bridge CFStringRef)[self.font fontName], [self.font pointSize], NULL);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTFontAttributeName, thisFont);
NSMutableArray *links = [NSMutableArray array];
NSMutableArray *textComponents = nil;
if (self.highlighted) textComponents = self.highlightedTextComponents;
else textComponents = self.textComponents;
for (RTLabelComponent *component in textComponents)
{
int index = (int)[textComponents indexOfObject:component];
component.componentIndex = index;
if ([component.tagLabel caseInsensitiveCompare:@"i"] == NSOrderedSame)
{
// make font italic
[self applyItalicStyleToText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"b"] == NSOrderedSame)
{
// make font bold
[self applyBoldStyleToText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"bi"] == NSOrderedSame)
{
[self applyBoldItalicStyleToText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"a"] == NSOrderedSame)
{
if (self.currentSelectedButtonComponentIndex==index)
{
if (self.selectedLinkAttributes)
{
[self applyFontAttributes:self.selectedLinkAttributes toText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else
{
[self applyBoldStyleToText:attrString atPosition:component.position withLength:(int)[component.text length]];
[self applyColor:@"#FF0000" toText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
}
else
{
if (self.linkAttributes)
{
[self applyFontAttributes:self.linkAttributes toText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else
{
[self applyBoldStyleToText:attrString atPosition:component.position withLength:(int)[component.text length]];
[self applySingleUnderlineText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
}
NSString *value = [component.attributes objectForKey:@"href"];
value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""];
[component.attributes setObject:value forKey:@"href"];
[links addObject:component];
}
else if ([component.tagLabel caseInsensitiveCompare:@"u"] == NSOrderedSame || [component.tagLabel caseInsensitiveCompare:@"uu"] == NSOrderedSame)
{
// underline
if ([component.tagLabel caseInsensitiveCompare:@"u"] == NSOrderedSame)
{
[self applySingleUnderlineText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"uu"] == NSOrderedSame)
{
[self applyDoubleUnderlineText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
if ([component.attributes objectForKey:@"color"])
{
NSString *value = [component.attributes objectForKey:@"color"];
[self applyUnderlineColor:value toText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
}
else if ([component.tagLabel caseInsensitiveCompare:@"font"] == NSOrderedSame)
{
[self applyFontAttributes:component.attributes toText:attrString atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"p"] == NSOrderedSame)
{
[self applyParagraphStyleToText:attrString attributes:component.attributes atPosition:component.position withLength:(int)[component.text length]];
}
else if ([component.tagLabel caseInsensitiveCompare:@"center"] == NSOrderedSame)
{
[self applyCenterStyleToText:attrString attributes:component.attributes atPosition:component.position withLength:(int)[component.text length]];
}
}
// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFRelease(attrString);
// Initialize a rectangular path.
CGMutablePathRef path = CGPathCreateMutable();
CGRect bounds = CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height);
CGPathAddRect(path, NULL, bounds);
// Create the frame and draw it into the graphics context
//CTFrameRef
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), path, NULL);
CFRange range;
CGSize constraint = CGSizeMake(self.frame.size.width, CGFLOAT_MAX);
self.optimumSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [self.plainText length]), nil, constraint, &range);
if (self.currentSelectedButtonComponentIndex==-1)
{
// only check for linkable items the first time, not when it's being redrawn on button pressed
for (RTLabelComponent *linkableComponents in links)
{
CGFloat height = 0.0;
CFArrayRef frameLines = CTFrameGetLines(frame);
for (CFIndex i=0; i<CFArrayGetCount(frameLines); i++)
{
CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(frameLines, i);
CFRange lineRange = CTLineGetStringRange(line);
CGFloat ascent;
CGFloat descent;
CGFloat leading;
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGPoint origin;
CTFrameGetLineOrigins(frame, CFRangeMake(i, 1), &origin);
if ( (linkableComponents.position<lineRange.location && linkableComponents.position+linkableComponents.text.length>(u_int16_t)(lineRange.location)) || (linkableComponents.position>=lineRange.location && linkableComponents.position<lineRange.location+lineRange.length))
{
CGFloat secondaryOffset;
CGFloat primaryOffset = CTLineGetOffsetForStringIndex(CFArrayGetValueAtIndex(frameLines,i), linkableComponents.position, &secondaryOffset);
CGFloat primaryOffset2 = CTLineGetOffsetForStringIndex(CFArrayGetValueAtIndex(frameLines,i), linkableComponents.position+linkableComponents.text.length, NULL);
CGFloat button_width = primaryOffset2 - primaryOffset;
RTLabelButton *button = [[RTLabelButton alloc] initWithFrame:CGRectMake(primaryOffset+origin.x, height, button_width, ascent+descent)];
[button setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]];
[button setComponentIndex:linkableComponents.componentIndex];
[button setUrl:[NSURL URLWithString:[linkableComponents.attributes objectForKey:@"href"]]];
[button addTarget:self action:@selector(onButtonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(onButtonTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
[button addTarget:self action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
origin.y = self.frame.size.height - origin.y;
height = origin.y + descent + _lineSpacing;
}
}
}
self.visibleRange = CTFrameGetVisibleStringRange(frame);
CFRelease(thisFont);
CFRelease(path);
CFRelease(styleDict1);
CFRelease(styleDict);
CFRelease(framesetter);
CTFrameDraw(frame, context);
CFRelease(frame);
}
#pragma mark -
#pragma mark styling
- (void)applyParagraphStyleToText:(CFMutableAttributedStringRef)text attributes:(NSMutableDictionary*)attributes atPosition:(int)position withLength:(int)length
{
CFMutableDictionaryRef styleDict = ( CFDictionaryCreateMutable( (0), 0, (0), (0) ) );
// direction
CTWritingDirection direction = kCTWritingDirectionLeftToRight;
// leading
CGFloat firstLineIndent = 0.0;
CGFloat headIndent = 0.0;
CGFloat tailIndent = 0.0;
CGFloat lineHeightMultiple = 1.0;
CGFloat maxLineHeight = 0;
CGFloat minLineHeight = 0;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
CTTextAlignment textAlignment = (CTTextAlignment)_textAlignment;
CTLineBreakMode lineBreakMode = (CTLineBreakMode)_lineBreakMode;
CGFloat lineSpacing = _lineSpacing;
for (NSUInteger i=0; i<[[attributes allKeys] count]; i++)
{
NSString *key = [[attributes allKeys] objectAtIndex:i];
id value = [attributes objectForKey:key];
if ([key caseInsensitiveCompare:@"align"] == NSOrderedSame)
{
if ([value caseInsensitiveCompare:@"left"] == NSOrderedSame)
{
textAlignment = kCTTextAlignmentLeft;
}
else if ([value caseInsensitiveCompare:@"right"] == NSOrderedSame)
{
textAlignment = kCTTextAlignmentRight;
}
else if ([value caseInsensitiveCompare:@"justify"] == NSOrderedSame)
{
textAlignment = kCTTextAlignmentJustified;
}
else if ([value caseInsensitiveCompare:@"center"] == NSOrderedSame)
{
textAlignment = kCTTextAlignmentCenter;
}
}
else if ([key caseInsensitiveCompare:@"indent"] == NSOrderedSame)
{
firstLineIndent = [value floatValue];
}
else if ([key caseInsensitiveCompare:@"linebreakmode"] == NSOrderedSame)
{
if ([value caseInsensitiveCompare:@"wordwrap"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByWordWrapping;
}
else if ([value caseInsensitiveCompare:@"charwrap"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByCharWrapping;
}
else if ([value caseInsensitiveCompare:@"clipping"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByClipping;
}
else if ([value caseInsensitiveCompare:@"truncatinghead"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByTruncatingHead;
}
else if ([value caseInsensitiveCompare:@"truncatingtail"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByTruncatingTail;
}
else if ([value caseInsensitiveCompare:@"truncatingmiddle"] == NSOrderedSame)
{
lineBreakMode = kCTLineBreakByTruncatingMiddle;
}
}
}
CTParagraphStyleSetting theSettings[] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &textAlignment },
{ kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode },
{ kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(CTWritingDirection), &direction },
{ kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
{ kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
{ kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineIndent },
{ kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent },
{ kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIndent },
{ kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(CGFloat), &lineHeightMultiple },
{ kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &maxLineHeight },
{ kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &minLineHeight },
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing },
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore }
};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, sizeof(theSettings) / sizeof(CTParagraphStyleSetting));
CFDictionaryAddValue( styleDict, kCTParagraphStyleAttributeName, theParagraphRef );
CFAttributedStringSetAttributes( text, CFRangeMake(position, length), styleDict, 0 );
CFRelease(theParagraphRef);
CFRelease(styleDict);
}
- (void)applyCenterStyleToText:(CFMutableAttributedStringRef)text attributes:(NSMutableDictionary*)attributes atPosition:(int)position withLength:(int)length
{
CFMutableDictionaryRef styleDict = ( CFDictionaryCreateMutable( (0), 0, (0), (0) ) );
// direction
CTWritingDirection direction = kCTWritingDirectionLeftToRight;
// leading
CGFloat firstLineIndent = 0.0;
CGFloat headIndent = 0.0;
CGFloat tailIndent = 0.0;
CGFloat lineHeightMultiple = 1.0;
CGFloat maxLineHeight = 0;
CGFloat minLineHeight = 0;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
int textAlignment = _textAlignment;
int lineBreakMode = _lineBreakMode;
int lineSpacing = (int)_lineSpacing;
textAlignment = kCTTextAlignmentCenter;
CTParagraphStyleSetting theSettings[] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &textAlignment },
{ kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode },
{ kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(CTWritingDirection), &direction },
{ kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing },
{ kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineIndent },
{ kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent },
{ kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIndent },
{ kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(CGFloat), &lineHeightMultiple },
{ kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &maxLineHeight },
{ kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &minLineHeight },
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing },
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore }
};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, sizeof(theSettings) / sizeof(CTParagraphStyleSetting));
CFDictionaryAddValue( styleDict, kCTParagraphStyleAttributeName, theParagraphRef );
CFAttributedStringSetAttributes( text, CFRangeMake(position, length), styleDict, 0 );
CFRelease(theParagraphRef);
CFRelease(styleDict);
}
- (void)applySingleUnderlineText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTUnderlineStyleAttributeName, (__bridge CFNumberRef)[NSNumber numberWithInt:kCTUnderlineStyleSingle]);
}
- (void)applyDoubleUnderlineText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTUnderlineStyleAttributeName, (__bridge CFNumberRef)[NSNumber numberWithInt:kCTUnderlineStyleDouble]);
}
- (void)applyItalicStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
CFTypeRef actualFontRef = CFAttributedStringGetAttribute(text, position, kCTFontAttributeName, NULL);
CTFontRef italicFontRef = CTFontCreateCopyWithSymbolicTraits(actualFontRef, 0.0, NULL, kCTFontItalicTrait, kCTFontItalicTrait);
if (!italicFontRef) {
//fallback to system italic font
UIFont *font = [UIFont italicSystemFontOfSize:CTFontGetSize(actualFontRef)];
italicFontRef = CTFontCreateWithName ((__bridge CFStringRef)[font fontName], [font pointSize], NULL);
}
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTFontAttributeName, italicFontRef);
CFRelease(italicFontRef);
}
- (void)applyFontAttributes:(NSDictionary*)attributes toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
for (NSString *key in attributes)
{
NSString *value = [attributes objectForKey:key];
value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""];
if ([key caseInsensitiveCompare:@"color"] == NSOrderedSame)
{
[self applyColor:value toText:text atPosition:position withLength:length];
}
else if ([key caseInsensitiveCompare:@"stroke"] == NSOrderedSame)
{
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTStrokeWidthAttributeName, (__bridge CFTypeRef)([NSNumber numberWithFloat:[[attributes objectForKey:@"stroke"] intValue]]));
}
else if ([key caseInsensitiveCompare:@"kern"] == NSOrderedSame)
{
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTKernAttributeName, (__bridge CFTypeRef)([NSNumber numberWithFloat:[[attributes objectForKey:@"kern"] intValue]]));
}
else if ([key caseInsensitiveCompare:@"underline"] == NSOrderedSame)
{
int numberOfLines = [value intValue];
if (numberOfLines==1)
{
[self applySingleUnderlineText:text atPosition:position withLength:length];
}
else if (numberOfLines==2)
{
[self applyDoubleUnderlineText:text atPosition:position withLength:length];
}
}
else if ([key caseInsensitiveCompare:@"style"] == NSOrderedSame)
{
if ([value caseInsensitiveCompare:@"bold"] == NSOrderedSame)
{
[self applyBoldStyleToText:text atPosition:position withLength:length];
}
else if ([value caseInsensitiveCompare:@"italic"] == NSOrderedSame)
{
[self applyItalicStyleToText:text atPosition:position withLength:length];
}
}
}
UIFont *font = nil;
if ([attributes objectForKey:@"face"] && [attributes objectForKey:@"size"])
{
NSString *fontName = [attributes objectForKey:@"face"];
fontName = [fontName stringByReplacingOccurrencesOfString:@"'" withString:@""];
font = [UIFont fontWithName:fontName size:[[attributes objectForKey:@"size"] intValue]];
}
else if ([attributes objectForKey:@"face"] && ![attributes objectForKey:@"size"])
{
NSString *fontName = [attributes objectForKey:@"face"];
fontName = [fontName stringByReplacingOccurrencesOfString:@"'" withString:@""];
font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
else if (![attributes objectForKey:@"face"] && [attributes objectForKey:@"size"])
{
font = [UIFont fontWithName:[self.font fontName] size:[[attributes objectForKey:@"size"] intValue]];
}
if (font)
{
CTFontRef customFont = CTFontCreateWithName ((__bridge CFStringRef)[font fontName], [font pointSize], NULL);
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTFontAttributeName, customFont);
CFRelease(customFont);
}
}
- (void)applyBoldStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
CFTypeRef actualFontRef = CFAttributedStringGetAttribute(text, position, kCTFontAttributeName, NULL);
CTFontRef boldFontRef = CTFontCreateCopyWithSymbolicTraits(actualFontRef, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait);
if (!boldFontRef) {
//fallback to system bold font
UIFont *font = [UIFont boldSystemFontOfSize:CTFontGetSize(actualFontRef)];
boldFontRef = CTFontCreateWithName ((__bridge CFStringRef)[font fontName], [font pointSize], NULL);
}
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTFontAttributeName, boldFontRef);
CFRelease(boldFontRef);
}
- (void)applyBoldItalicStyleToText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
CFTypeRef actualFontRef = CFAttributedStringGetAttribute(text, position, kCTFontAttributeName, NULL);
CTFontRef boldItalicFontRef = CTFontCreateCopyWithSymbolicTraits(actualFontRef, 0.0, NULL, kCTFontBoldTrait | kCTFontItalicTrait , kCTFontBoldTrait | kCTFontItalicTrait);
if (!boldItalicFontRef) {
//try fallback to system boldItalic font
NSString *fontName = [NSString stringWithFormat:@"%@-BoldOblique", self.font.fontName];
boldItalicFontRef = CTFontCreateWithName ((__bridge CFStringRef)fontName, [self.font pointSize], NULL);
}
if (boldItalicFontRef) {
CFAttributedStringSetAttribute(text, CFRangeMake(position, length), kCTFontAttributeName, boldItalicFontRef);
CFRelease(boldItalicFontRef);
}
}
- (void)applyColor:(NSString*)value toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
if ([value rangeOfString:@"#"].location==0)
{
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
value = [value stringByReplacingOccurrencesOfString:@"#" withString:@""];
NSArray *colorComponents = [self colorForHex:value];
CGFloat components[] = { [[colorComponents objectAtIndex:0] floatValue] , [[colorComponents objectAtIndex:1] floatValue] , [[colorComponents objectAtIndex:2] floatValue] , [[colorComponents objectAtIndex:3] floatValue] };
CGColorRef color = CGColorCreate(rgbColorSpace, components);
CFAttributedStringSetAttribute(text, CFRangeMake(position, length),kCTForegroundColorAttributeName, color);
CFRelease(color);
CGColorSpaceRelease(rgbColorSpace);
} else {
value = [value stringByAppendingString:@"Color"];
SEL colorSel = NSSelectorFromString(value);
UIColor *_color = nil;
if ([UIColor respondsToSelector:colorSel]) {
_color = [UIColor performSelector:colorSel];
CGColorRef color = [_color CGColor];
CFAttributedStringSetAttribute(text, CFRangeMake(position, length),kCTForegroundColorAttributeName, color);
}
}
}
- (void)applyUnderlineColor:(NSString*)value toText:(CFMutableAttributedStringRef)text atPosition:(int)position withLength:(int)length
{
value = [value stringByReplacingOccurrencesOfString:@"'" withString:@""];
if ([value rangeOfString:@"#"].location==0) {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
value = [value stringByReplacingOccurrencesOfString:@"#" withString:@"0x"];
NSArray *colorComponents = [self colorForHex:value];
CGFloat components[] = { [[colorComponents objectAtIndex:0] floatValue] , [[colorComponents objectAtIndex:1] floatValue] , [[colorComponents objectAtIndex:2] floatValue] , [[colorComponents objectAtIndex:3] floatValue] };
CGColorRef color = CGColorCreate(rgbColorSpace, components);
CFAttributedStringSetAttribute(text, CFRangeMake(position, length),kCTUnderlineColorAttributeName, color);
CGColorRelease(color);
CGColorSpaceRelease(rgbColorSpace);
}
else
{
value = [value stringByAppendingString:@"Color"];
SEL colorSel = NSSelectorFromString(value);
if ([UIColor respondsToSelector:colorSel]) {
UIColor *_color = [UIColor performSelector:colorSel];
CGColorRef color = [_color CGColor];
CFAttributedStringSetAttribute(text, CFRangeMake(position, length),kCTUnderlineColorAttributeName, color);
//CGColorRelease(color);
}
}
}
#pragma mark -
#pragma mark button
- (void)onButtonTouchDown:(id)sender
{
RTLabelButton *button = (RTLabelButton*)sender;
[self setCurrentSelectedButtonComponentIndex:button.componentIndex];
[self setNeedsDisplay];
}
- (void)onButtonTouchUpOutside:(id)sender
{
[self setCurrentSelectedButtonComponentIndex:-1];
[self setNeedsDisplay];
}
- (void)onButtonPressed:(id)sender
{
RTLabelButton *button = (RTLabelButton*)sender;
[self setCurrentSelectedButtonComponentIndex:-1];
[self setNeedsDisplay];
if ([self.delegate respondsToSelector:@selector(rtLabel:didSelectLinkWithURL:)])
{
[self.delegate rtLabel:self didSelectLinkWithURL:button.url];
}
}
- (CGSize)optimumSize
{
[self render];
return _optimumSize;
}
- (void)setLineSpacing:(CGFloat)lineSpacing
{
_lineSpacing = lineSpacing;
[self setNeedsDisplay];
}
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted!=_highlighted)
{
_highlighted = highlighted;
[self setNeedsDisplay];
}
}
- (void)setHighlightedText:(NSString *)text
{
_highlightedText = [text stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
RTLabelExtractedComponent *component = [RTLabel extractTextStyleFromText:_highlightedText paragraphReplacement:self.paragraphReplacement];
[self setHighlightedTextComponents:component.textComponents];
}
- (void)setText:(NSString *)text
{
_text = [text stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
RTLabelExtractedComponent *component = [RTLabel extractTextStyleFromText:_text paragraphReplacement:self.paragraphReplacement];
[self setTextComponents:component.textComponents];
[self setPlainText:component.plainText];
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text extractedTextComponent:(RTLabelExtractedComponent*)extractedComponent
{
_text = [text stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
[self setTextComponents:extractedComponent.textComponents];
[self setPlainText:extractedComponent.plainText];
[self setNeedsDisplay];
}
- (void)setHighlightedText:(NSString *)text extractedTextComponent:(RTLabelExtractedComponent*)extractedComponent
{
_highlightedText = [text stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
[self setHighlightedTextComponents:extractedComponent.textComponents];
}
// http://forums.macrumors.com/showthread.php?t=925312
// not accurate
- (CGFloat)frameHeight:(CTFrameRef)theFrame
{
CFArrayRef lines = CTFrameGetLines(theFrame);
CGFloat height = 0.0;
CGFloat ascent, descent, leading;
for (CFIndex index = 0; index < CFArrayGetCount(lines); index++) {
CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(lines, index);
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
height += (ascent + fabs(descent) + leading);
}
return ceilf((float)height);
}
- (void)dealloc
{
self.delegate = nil;
}
- (NSArray *)components
{
NSScanner *scanner = [NSScanner scannerWithString:self.text];
[scanner setCharactersToBeSkipped:nil];
NSMutableArray *components = [NSMutableArray array];
while (![scanner isAtEnd])
{
NSString *currentComponent;
BOOL foundComponent = [scanner scanUpToString:@"http" intoString:¤tComponent];
if (foundComponent)
{
[components addObject:currentComponent];
NSString *string;
BOOL foundURLComponent = [scanner scanUpToString:@" " intoString:&string];
if (foundURLComponent)
{
// if last character of URL is punctuation, its probably not part of the URL
NSCharacterSet *punctuationSet = [NSCharacterSet punctuationCharacterSet];
NSInteger lastCharacterIndex = string.length - 1;
if ([punctuationSet characterIsMember:[string characterAtIndex:lastCharacterIndex]])
{
// remove the punctuation from the URL string and move the scanner back
string = [string substringToIndex:lastCharacterIndex];
[scanner setScanLocation:scanner.scanLocation - 1];
}
[components addObject:string];
}
}
else
{ // first string is a link
NSString *string;
BOOL foundURLComponent = [scanner scanUpToString:@" " intoString:&string];
if (foundURLComponent)
{
[components addObject:string];
}
}
}
return [components copy];
}
+ (RTLabelExtractedComponent*)extractTextStyleFromText:(NSString*)data paragraphReplacement:(NSString*)paragraphReplacement
{
NSScanner *scanner = nil;
NSString *text = nil;
NSString *tag = nil;
NSMutableArray *components = [NSMutableArray array];
int last_position = 0;
scanner = [NSScanner scannerWithString:data];
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<" intoString:NULL];
[scanner scanUpToString:@">" intoString:&text];
NSString *delimiter = [NSString stringWithFormat:@"%@>", text];
int position = (int)[data rangeOfString:delimiter options:NSCaseInsensitiveSearch range:NSMakeRange(last_position, [data length] - last_position)].location;
if (position != (int)NSNotFound && position != -1)
{
if ([delimiter rangeOfString:@"<p"].location==0)
{
data = [data stringByReplacingOccurrencesOfString:delimiter withString:paragraphReplacement options:NSCaseInsensitiveSearch range:NSMakeRange(position, delimiter.length)];
}
else
{
data = [data stringByReplacingOccurrencesOfString:delimiter withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(position, delimiter.length)];
}
data = [data stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
data = [data stringByReplacingOccurrencesOfString:@">" withString:@">"];
}
if ([text rangeOfString:@"</"].location==0)
{
// end of tag
tag = [text substringFromIndex:2];
if (position != (int)NSNotFound)
{
for (int i=(int)([components count]-1); i>=0; i--)
{
RTLabelComponent *component = [components objectAtIndex:i];
if (component.text==nil && [component.tagLabel isEqualToString:tag])
{
NSString *text2 = [data substringWithRange:NSMakeRange(component.position, position-component.position)];
component.text = text2;
break;
}
}
}
}
else
{
// start of tag
NSArray *textComponents = [[text substringFromIndex:1] componentsSeparatedByString:@" "];
tag = [textComponents objectAtIndex:0];
//NSLog(@"start of tag: %@", tag);
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
for (NSUInteger i=1; i<[textComponents count]; i++)
{
NSArray *pair = [[textComponents objectAtIndex:i] componentsSeparatedByString:@"="];
if ([pair count] > 0) {
NSString *key = [[pair objectAtIndex:0] lowercaseString];
if ([pair count]>=2) {
// Trim " charactere
NSString *value = [[pair subarrayWithRange:NSMakeRange(1, [pair count] - 1)] componentsJoinedByString:@"="];
value = [value stringByReplacingOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, 1)];
value = [value stringByReplacingOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange([value length]-1, 1)];
[attributes setObject:value forKey:key];
} else if ([pair count]==1) {
[attributes setObject:key forKey:key];
}
}
}
RTLabelComponent *component = [RTLabelComponent componentWithString:nil tag:tag attributes:attributes];
component.position = position;
[components addObject:component];
}
last_position = position;
}
return [RTLabelExtractedComponent rtLabelExtractComponentsWithTextComponent:components plainText:data];
}
- (void)parse:(NSString *)data valid_tags:(NSArray *)valid_tags
{
//use to strip the HTML tags from the data
NSScanner *scanner = nil;
NSString *text = nil;
NSString *tag = nil;
NSMutableArray *components = [NSMutableArray array];
//set up the scanner
scanner = [NSScanner scannerWithString:data];
NSMutableDictionary *lastAttributes = nil;
int last_position = 0;
while([scanner isAtEnd] == NO)
{
//find start of tag
[scanner scanUpToString:@"<" intoString:NULL];
//find end of tag
[scanner scanUpToString:@">" intoString:&text];
NSMutableDictionary *attributes = nil;
//get the name of the tag
if([text rangeOfString:@"</"].location != NSNotFound)
tag = [text substringFromIndex:2]; //remove </
else
{
tag = [text substringFromIndex:1]; //remove <
//find out if there is a space in the tag
if([tag rangeOfString:@" "].location != NSNotFound)
{
attributes = [NSMutableDictionary dictionary];
NSArray *rawAttributes = [tag componentsSeparatedByString:@" "];
for (NSUInteger i=1; i<[rawAttributes count]; i++)
{
NSArray *pair = [[rawAttributes objectAtIndex:i] componentsSeparatedByString:@"="];
if ([pair count]==2)
{
[attributes setObject:[pair objectAtIndex:1] forKey:[pair objectAtIndex:0]];
}
}
//remove text after a space
tag = [tag substringToIndex:[tag rangeOfString:@" "].location];
}
}
//if not a valid tag, replace the tag with a space
if([valid_tags containsObject:tag] == NO)
{
NSString *delimiter = [NSString stringWithFormat:@"%@>", text];
int position = (int)[data rangeOfString:delimiter].location;
BOOL isEnd = [delimiter rangeOfString:@"</"].location!=NSNotFound;
if (position != (int)NSNotFound)