/
DTAttributedTextContentView.m
884 lines (713 loc) · 25.2 KB
/
DTAttributedTextContentView.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
//
// DTAttributedTextContentView.h
// DTCoreText
//
// Created by Oliver Drobnik on 1/9/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#import "DTAttributedTextContentView.h"
#import "DTCoreText.h"
#import <QuartzCore/QuartzCore.h>
#if !__has_feature(objc_arc)
#error THIS CODE MUST BE COMPILED WITH ARC ENABLED!
#endif
NSString * const DTAttributedTextContentViewDidFinishLayoutNotification = @"DTAttributedTextContentViewDidFinishLayoutNotification";
@interface DTAttributedTextContentView ()
{
BOOL _shouldDrawImages;
BOOL _shouldDrawLinks;
BOOL _shouldLayoutCustomSubviews;
DTAttributedTextContentViewRelayoutMask _relayoutMask;
NSMutableSet *customViews;
NSMutableDictionary *customViewsForLinksIndex;
BOOL _isTiling;
BOOL _layoutFrameHeightIsConstrainedByBounds;
DTCoreTextLayouter *_layouter;
CGPoint _layoutOffset;
CGSize _backgroundOffset;
// lookup bitmask what delegate methods are implemented
struct
{
unsigned int delegateSupportsCustomViewsForAttachments:1;
unsigned int delegateSupportsCustomViewsForLinks:1;
unsigned int delegateSupportsGenericCustomViews:1;
unsigned int delegateSupportsNotificationAfterDrawing:1;
unsigned int delegateSupportsNotificationBeforeTextBoxDrawing:1;
} _delegateFlags;
__unsafe_unretained id <DTAttributedTextContentViewDelegate> _delegate;
}
@property (nonatomic, strong) NSMutableDictionary *customViewsForLinksIndex;
@property (nonatomic, strong) NSMutableDictionary *customViewsForAttachmentsIndex;
@property (nonatomic, strong) NSMutableSet *customViews;
- (void)removeAllCustomViews;
- (void)removeAllCustomViewsForLinks;
- (void)removeSubviewsOutsideRect:(CGRect)rect;
@end
static Class _layerClassToUseForDTAttributedTextContentView = nil;
@implementation DTAttributedTextContentView (Tiling)
+ (void)setLayerClass:(Class)layerClass
{
_layerClassToUseForDTAttributedTextContentView = layerClass;
}
+ (Class)layerClass
{
if (_layerClassToUseForDTAttributedTextContentView)
{
return _layerClassToUseForDTAttributedTextContentView;
}
return [CALayer class];
}
@end
@implementation DTAttributedTextContentView
- (void)setup
{
self.contentMode = UIViewContentModeTopLeft; // to avoid bitmap scaling effect on resize
_shouldLayoutCustomSubviews = YES;
// by default we draw images, if custom views are supported (by setting delegate) this is disabled
// if you still want images to be drawn together with text then set it back to YES after setting delegate
_shouldDrawImages = YES;
// by default we draw links. If you don't want that because you want to highlight the text in
// DTLinkButton set this property to NO and create a highlighted version of the attributed string
_shouldDrawLinks = YES;
_layoutFrameHeightIsConstrainedByBounds = NO; // we calculate the necessary height unemcumbered by bounds
_relayoutMask = DTAttributedTextContentViewRelayoutOnWidthChanged;
// possibly already set in NIB
if (!self.backgroundColor)
{
self.backgroundColor = [DTColor whiteColor];
}
// set tile size if applicable
CATiledLayer *layer = (id)self.layer;
if ([layer isKindOfClass:[CATiledLayer class]])
{
// get larger dimension and multiply by scale
UIScreen *mainScreen = [UIScreen mainScreen];
CGFloat largerDimension = MAX(mainScreen.applicationFrame.size.width, mainScreen.applicationFrame.size.height);
CGFloat scale = mainScreen.scale;
// this way tiles cover entire screen regardless of orientation or scale
CGSize tileSize = CGSizeMake(largerDimension * scale, largerDimension * scale);
layer.tileSize = tileSize;
_isTiling = YES;
}
}
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[self setup];
}
- (void)dealloc
{
[self removeAllCustomViews];
}
- (NSString *)debugDescription
{
NSString *extract = [[[_layoutFrame attributedStringFragment] string] substringFromIndex:[self.layoutFrame visibleStringRange].location];
if ([extract length]>10)
{
extract = [extract substringToIndex:10];
}
return [NSString stringWithFormat:@"<%@ %@ range:%@ '%@...'>", [self class], NSStringFromCGRect(self.frame),NSStringFromRange([self.layoutFrame visibleStringRange]), extract];
}
- (void)layoutSubviewsInRect:(CGRect)rect
{
// if we are called for partial (non-infinate) we remove unneeded custom subviews first
if (!CGRectIsInfinite(rect))
{
[self removeSubviewsOutsideRect:rect];
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
DTCoreTextLayoutFrame *theLayoutFrame = self.layoutFrame;
NSAttributedString *layoutString = [theLayoutFrame attributedStringFragment];
NSArray *lines;
if (CGRectIsInfinite(rect))
{
lines = [theLayoutFrame lines];
}
else
{
lines = [theLayoutFrame linesVisibleInRect:rect];
}
// hide all customViews
for (UIView *view in self.customViews)
{
view.hidden = YES;
}
for (DTCoreTextLayoutLine *oneLine in lines)
{
NSRange lineRange = [oneLine stringRange];
NSUInteger skipRunsBeforeLocation = 0;
for (DTCoreTextGlyphRun *oneRun in oneLine.glyphRuns)
{
// add custom views if necessary
NSRange runRange = [oneRun stringRange];
CGRect frameForSubview = CGRectZero;
if (runRange.location>=skipRunsBeforeLocation)
{
// see if it's a link
NSRange effectiveRangeOfLink;
NSRange effectiveRangeOfAttachment;
// make sure that a link is only as long as the area to the next attachment or the current attachment itself
DTTextAttachment *attachment = [layoutString attribute:NSAttachmentAttributeName atIndex:runRange.location longestEffectiveRange:&effectiveRangeOfAttachment inRange:lineRange];
// if there is no attachment then the effectiveRangeOfAttachment contains the range until the next attachment
NSURL *linkURL = [layoutString attribute:DTLinkAttribute atIndex:runRange.location longestEffectiveRange:&effectiveRangeOfLink inRange:effectiveRangeOfAttachment];
// avoid chaining together glyph runs for an attachment
if (linkURL && !attachment)
{
// compute bounding frame over potentially multiple (chinese) glyphs
skipRunsBeforeLocation = effectiveRangeOfLink.location+effectiveRangeOfLink.length;
// make one link view for all glyphruns in this line
frameForSubview = [oneLine frameOfGlyphsWithRange:effectiveRangeOfLink];
runRange = effectiveRangeOfLink;
}
else
{
// individual glyph run
if (attachment)
{
// frame might be different due to image vertical alignment
CGFloat ascender = [attachment ascentForLayout];
CGFloat descender = [attachment descentForLayout];
frameForSubview = CGRectMake(oneRun.frame.origin.x, oneLine.baselineOrigin.y - ascender, oneRun.frame.size.width, ascender+descender);
}
else
{
frameForSubview = oneRun.frame;
}
}
if (CGRectIsEmpty(frameForSubview))
{
continue;
}
NSNumber *indexKey = [NSNumber numberWithInteger:runRange.location];
// offset layout if necessary
if (!CGPointEqualToPoint(_layoutOffset, CGPointZero))
{
frameForSubview.origin.x += _layoutOffset.x;
frameForSubview.origin.y += _layoutOffset.y;
}
// round frame
frameForSubview.origin.x = floorf(frameForSubview.origin.x);
frameForSubview.origin.y = ceilf(frameForSubview.origin.y);
frameForSubview.size.width = roundf(frameForSubview.size.width);
frameForSubview.size.height = roundf(frameForSubview.size.height);
if (CGRectGetMinY(frameForSubview)> CGRectGetMaxY(rect) || CGRectGetMaxY(frameForSubview) < CGRectGetMinY(rect))
{
// is still outside even though the bounds of the line already intersect visible area
continue;
}
if (_delegateFlags.delegateSupportsCustomViewsForAttachments || _delegateFlags.delegateSupportsGenericCustomViews)
{
if (attachment)
{
indexKey = [NSNumber numberWithInteger:[attachment hash]];
UIView *existingAttachmentView = [self.customViewsForAttachmentsIndex objectForKey:indexKey];
if (existingAttachmentView)
{
//dispatch_sync(dispatch_get_main_queue(), ^{
existingAttachmentView.hidden = NO;
existingAttachmentView.frame = frameForSubview;
existingAttachmentView.alpha = 1;
[existingAttachmentView setNeedsLayout];
[existingAttachmentView setNeedsDisplay];
//});
linkURL = nil; // prevent adding link button on top of image view
}
else
{
UIView *newCustomAttachmentView = nil;
if ([attachment isKindOfClass:[DTDictationPlaceholderTextAttachment class]])
{
newCustomAttachmentView = [DTDictationPlaceholderView placeholderView];
newCustomAttachmentView.frame = frameForSubview; // set fixed frame
}
else if (_delegateFlags.delegateSupportsCustomViewsForAttachments)
{
newCustomAttachmentView = [_delegate attributedTextContentView:self viewForAttachment:attachment frame:frameForSubview];
}
else if (_delegateFlags.delegateSupportsGenericCustomViews)
{
NSAttributedString *string = [layoutString attributedSubstringFromRange:runRange];
newCustomAttachmentView = [_delegate attributedTextContentView:self viewForAttributedString:string frame:frameForSubview];
}
if (newCustomAttachmentView)
{
// delegate responsible to set frame
if (newCustomAttachmentView)
{
newCustomAttachmentView.tag = [indexKey integerValue];
[self addSubview:newCustomAttachmentView];
[self.customViews addObject:newCustomAttachmentView];
[self.customViewsForAttachmentsIndex setObject:newCustomAttachmentView forKey:indexKey];
linkURL = nil; // prevent adding link button on top of image view
}
}
}
}
}
if (linkURL && (_delegateFlags.delegateSupportsCustomViewsForLinks || _delegateFlags.delegateSupportsGenericCustomViews))
{
UIView *existingLinkView = [self.customViewsForLinksIndex objectForKey:indexKey];
// make sure that the frame height is no less than the line height for hyperlinks
if (frameForSubview.size.height < oneLine.frame.size.height)
{
frameForSubview.origin.y = truncf(oneLine.frame.origin.y);
frameForSubview.size.height = ceilf(oneLine.frame.size.height);
}
if (existingLinkView)
{
existingLinkView.frame = frameForSubview;
existingLinkView.hidden = NO;
}
else
{
UIView *newCustomLinkView = nil;
// make sure that the frame height is no less than the line height for hyperlinks
if (frameForSubview.size.height < oneLine.frame.size.height)
{
frameForSubview.origin.y = truncf(oneLine.frame.origin.y);
frameForSubview.size.height = ceilf(oneLine.frame.size.height);
}
if (_delegateFlags.delegateSupportsCustomViewsForLinks)
{
NSDictionary *attributes = [layoutString attributesAtIndex:runRange.location effectiveRange:NULL];
NSString *guid = [attributes objectForKey:DTGUIDAttribute];
newCustomLinkView = [_delegate attributedTextContentView:self viewForLink:linkURL identifier:guid frame:frameForSubview];
}
else if (_delegateFlags.delegateSupportsGenericCustomViews)
{
NSAttributedString *string = [layoutString attributedSubstringFromRange:runRange];
newCustomLinkView = [_delegate attributedTextContentView:self viewForAttributedString:string frame:frameForSubview];
}
// delegate responsible to set frame
if (newCustomLinkView)
{
newCustomLinkView.tag = runRange.location;
[self addSubview:newCustomLinkView];
[self.customViews addObject:newCustomLinkView];
[self.customViewsForLinksIndex setObject:newCustomLinkView forKey:indexKey];
}
}
}
}
}
}
[CATransaction commit];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (_shouldLayoutCustomSubviews)
{
[self layoutSubviewsInRect:CGRectInfinite];
}
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
// needs clearing of background
CGRect rect = CGContextGetClipBoundingBox(ctx);
if (_backgroundOffset.height || _backgroundOffset.width)
{
CGContextSetPatternPhase(ctx, _backgroundOffset);
}
CGContextSetFillColorWithColor(ctx, [self.backgroundColor CGColor]);
CGContextFillRect(ctx, rect);
// offset layout if necessary
if (!CGPointEqualToPoint(_layoutOffset, CGPointZero))
{
CGAffineTransform transform = CGAffineTransformMakeTranslation(_layoutOffset.x, _layoutOffset.y);
CGContextConcatCTM(ctx, transform);
}
DTCoreTextLayoutFrame *theLayoutFrame = self.layoutFrame; // this is synchronized
// construct drawing options
DTCoreTextLayoutFrameDrawingOptions options = DTCoreTextLayoutFrameDrawingDefault;
if (!_shouldDrawImages)
{
options |= DTCoreTextLayoutFrameDrawingOmitAttachments;
}
if (!_shouldDrawImages)
{
options |= DTCoreTextLayoutFrameDrawingOmitLinks;
}
// need to prevent updating of string and drawing at the same time
[theLayoutFrame drawInContext:ctx options:options];
if (_delegateFlags.delegateSupportsNotificationAfterDrawing)
{
[_delegate attributedTextContentView:self didDrawLayoutFrame:theLayoutFrame inContext:ctx];
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layoutFrame drawInContext:context options:DTCoreTextLayoutFrameDrawingDefault];
}
- (void)relayoutText
{
// Make sure we actually have a superview and a previous layout before attempting to relayout the text.
if (_layoutFrame && self.superview)
{
// need new layout frame, layouter can remain because the attributed string is probably the same
self.layoutFrame = nil;
// remove all links because they might have merged or split
[self removeAllCustomViewsForLinks];
if (_attributedString)
{
// triggers new layout
CGSize neededSize = [self intrinsicContentSize];
CGRect optimalFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, neededSize.width, neededSize.height);
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSValue valueWithCGRect:optimalFrame] forKey:@"OptimalFrame"];
[[NSNotificationCenter defaultCenter] postNotificationName:DTAttributedTextContentViewDidFinishLayoutNotification object:self userInfo:userInfo];
}
[self setNeedsLayout];
[self setNeedsDisplayInRect:self.bounds];
}
}
- (void)removeAllCustomViewsForLinks
{
NSArray *linkViews = [customViewsForLinksIndex allValues];
for (UIView *customView in linkViews)
{
[customView removeFromSuperview];
[customViews removeObject:customView];
}
[customViewsForLinksIndex removeAllObjects];
}
- (void)removeAllCustomViews
{
NSSet *allCustomViews = [NSSet setWithSet:customViews];
for (UIView *customView in allCustomViews)
{
[customView removeFromSuperview];
[customViews removeObject:customView];
}
[customViewsForAttachmentsIndex removeAllObjects];
[customViewsForLinksIndex removeAllObjects];
}
- (void)removeSubviewsOutsideRect:(CGRect)rect
{
NSSet *allCustomViews = [NSSet setWithSet:customViews];
for (UIView *customView in allCustomViews)
{
if (CGRectGetMinY(customView.frame)> CGRectGetMaxY(rect) || CGRectGetMaxY(customView.frame) < CGRectGetMinY(rect))
{
NSNumber *indexKey = [NSNumber numberWithInteger:customView.tag];
[customView removeFromSuperview];
[customViews removeObject:customView];
[customViewsForAttachmentsIndex removeObjectForKey:indexKey];
[customViewsForLinksIndex removeObjectForKey:indexKey];
}
}
}
#pragma mark - Sizing
- (CGSize)intrinsicContentSize
{
if (!self.layoutFrame) // creates new layout frame if possible
{
return CGSizeMake(-1, -1); // UIViewNoIntrinsicMetric as of iOS 6
}
// we have a layout frame and from this we get the needed size
return CGSizeMake(_layoutFrame.frame.size.width + _edgeInsets.left + _edgeInsets.right, CGRectGetMaxY(_layoutFrame.frame) + _edgeInsets.bottom);
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize neededSize = [self intrinsicContentSize]; // creates layout frame if necessary
if (neededSize.width>=0 && neededSize.height>=0)
{
return neededSize;
}
return size;
}
- (CGRect)_frameForLayoutFrameConstraintedToWidth:(CGFloat)constrainWidth
{
if (!isnormal(constrainWidth))
{
constrainWidth = self.bounds.size.width;
}
CGRect bounds = self.bounds;
bounds.size.width = constrainWidth;
CGRect rect = UIEdgeInsetsInsetRect(bounds, _edgeInsets);
if (rect.size.width<=0)
{
// cannot create layout frame with negative or zero width
return CGRectZero;
}
if (_layoutFrameHeightIsConstrainedByBounds)
{
if (rect.size.height<=0)
{
// cannot create layout frame with negative or zero height if flexible height is disabled
return CGRectZero;
}
// already set height to bounds height
}
else
{
rect.size.height = CGFLOAT_OPEN_HEIGHT; // necessary height set as soon as we know it.
}
return rect;
}
- (CGSize)suggestedFrameSizeToFitEntireStringConstraintedToWidth:(CGFloat)width
{
// create a temporary frame, will be cached by the layouter for the given rect
CGRect rect = [self _frameForLayoutFrameConstraintedToWidth:width];
DTCoreTextLayoutFrame *tmpLayoutFrame = [self.layouter layoutFrameWithRect:rect range:NSMakeRange(0, 0)];
// we have a layout frame and from this we get the needed size
return CGSizeMake(tmpLayoutFrame.frame.size.width + _edgeInsets.left + _edgeInsets.right, CGRectGetMaxY(tmpLayoutFrame.frame) + _edgeInsets.bottom);
}
#pragma mark Properties
- (void)setEdgeInsets:(UIEdgeInsets)edgeInsets
{
if (!UIEdgeInsetsEqualToEdgeInsets(edgeInsets, _edgeInsets))
{
_edgeInsets = edgeInsets;
[self relayoutText];
}
}
- (void)setAttributedString:(NSAttributedString *)string
{
if (_attributedString != string)
{
// keep the layouter, update string
self.layouter.attributedString = string;
_attributedString = [string copy];
// only do relayout if there is a previous layout frame and visible
if (_layoutFrame)
{
// new layout invalidates all positions for custom views
[self removeAllCustomViews];
// relayout only occurs if the view is visible
[self relayoutText];
}
else
{
// this is needed or else no lazy layout will be triggered if there is no layout frame yet (before this is added to a superview)
[self setNeedsLayout];
[self setNeedsDisplayInRect:self.bounds];
}
}
}
- (void)setFrame:(CGRect)frame
{
CGRect oldFrame = self.frame;
[super setFrame:frame];
if (!_layoutFrame)
{
return;
}
// having a layouter means we are responsible for layouting yourselves
// relayout based on relayoutMask
BOOL shouldRelayout = NO;
if (_relayoutMask & DTAttributedTextContentViewRelayoutOnHeightChanged)
{
if (oldFrame.size.height != frame.size.height)
{
shouldRelayout = YES;
}
}
if (_relayoutMask & DTAttributedTextContentViewRelayoutOnWidthChanged)
{
if (oldFrame.size.width != frame.size.width)
{
shouldRelayout = YES;
}
}
if (shouldRelayout)
{
[self relayoutText];
}
if (oldFrame.size.height < frame.size.height)
{
// need to draw the newly visible area
[self setNeedsDisplayInRect:CGRectMake(0, oldFrame.size.height, self.bounds.size.width, frame.size.height - oldFrame.size.height)];
}
}
- (void)setShouldDrawImages:(BOOL)shouldDrawImages
{
if (_shouldDrawImages != shouldDrawImages)
{
_shouldDrawImages = shouldDrawImages;
[self setNeedsDisplay];
}
}
- (void)setShouldDrawLinks:(BOOL)shouldDrawLinks
{
if (_shouldDrawLinks != shouldDrawLinks)
{
_shouldDrawLinks = shouldDrawLinks;
[self setNeedsDisplay];
}
}
- (void)setBackgroundColor:(DTColor *)newColor
{
super.backgroundColor = newColor;
if ([newColor alphaComponent]<1.0)
{
self.opaque = NO;
}
else
{
self.opaque = YES;
}
}
- (DTCoreTextLayouter *)layouter
{
@synchronized(self)
{
if (!_layouter)
{
if (_attributedString)
{
_layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:_attributedString];
// allow frame caching if somebody uses the suggestedSize
_layouter.shouldCacheLayoutFrames = YES;
}
}
return _layouter;
}
}
- (void)setLayouter:(DTCoreTextLayouter *)layouter
{
@synchronized(self)
{
if (_layouter != layouter)
{
_layouter = layouter;
}
}
}
- (DTCoreTextLayoutFrame *)layoutFrame
{
@synchronized(self)
{
DTCoreTextLayouter *theLayouter = self.layouter;
if (!_layoutFrame)
{
// we can only layout if we have our own layouter
if (theLayouter)
{
CGRect rect = UIEdgeInsetsInsetRect(self.bounds, _edgeInsets);
if (rect.size.width<=0)
{
// cannot create layout frame with negative or zero width
return nil;
}
if (_layoutFrameHeightIsConstrainedByBounds)
{
if (rect.size.height<=0)
{
// cannot create layout frame with negative or zero height if flexible height is disabled
return nil;
}
// height already set
}
else
{
rect.size.height = CGFLOAT_OPEN_HEIGHT; // necessary height set as soon as we know it.
}
_layoutFrame = [theLayouter layoutFrameWithRect:rect range:NSMakeRange(0, 0)];
// this must have been the initial layout pass
CGSize neededSize = CGSizeMake(_layoutFrame.frame.size.width + _edgeInsets.left + _edgeInsets.right, CGRectGetMaxY(_layoutFrame.frame) + _edgeInsets.bottom);
CGRect optimalFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, neededSize.width, neededSize.height);
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSValue valueWithCGRect:optimalFrame] forKey:@"OptimalFrame"];
[[NSNotificationCenter defaultCenter] postNotificationName:DTAttributedTextContentViewDidFinishLayoutNotification object:self userInfo:userInfo];
if (_delegateFlags.delegateSupportsNotificationBeforeTextBoxDrawing)
{
__unsafe_unretained DTAttributedTextContentView *weakself = self;
[_layoutFrame setTextBlockHandler:^(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground) {
BOOL result = [weakself->_delegate attributedTextContentView:weakself shouldDrawBackgroundForTextBlock:textBlock frame:frame context:context forLayoutFrame:weakself->_layoutFrame];
if (shouldDrawDefaultBackground)
{
*shouldDrawDefaultBackground = result;
}
}];
}
}
}
return _layoutFrame;
}
}
- (void)setLayoutFrame:(DTCoreTextLayoutFrame *)layoutFrame
{
@synchronized(self)
{
if (_layoutFrame != layoutFrame)
{
[self removeAllCustomViewsForLinks];
if (layoutFrame)
{
[self setNeedsLayout];
[self setNeedsDisplayInRect:self.bounds];
}
_layoutFrame = layoutFrame;
}
};
}
- (NSMutableSet *)customViews
{
if (!customViews)
{
customViews = [[NSMutableSet alloc] init];
}
return customViews;
}
- (NSMutableDictionary *)customViewsForLinksIndex
{
if (!customViewsForLinksIndex)
{
customViewsForLinksIndex = [[NSMutableDictionary alloc] init];
}
return customViewsForLinksIndex;
}
- (NSMutableDictionary *)customViewsForAttachmentsIndex
{
if (!customViewsForAttachmentsIndex)
{
customViewsForAttachmentsIndex = [[NSMutableDictionary alloc] init];
}
return customViewsForAttachmentsIndex;
}
- (void)setDelegate:(id<DTAttributedTextContentViewDelegate>)delegate
{
_delegate = delegate;
_delegateFlags.delegateSupportsCustomViewsForAttachments = [_delegate respondsToSelector:@selector(attributedTextContentView:viewForAttachment:frame:)];
_delegateFlags.delegateSupportsCustomViewsForLinks = [_delegate respondsToSelector:@selector(attributedTextContentView:viewForLink:identifier:frame:)];
_delegateFlags.delegateSupportsGenericCustomViews = [_delegate respondsToSelector:@selector(attributedTextContentView:viewForAttributedString:frame:)];
_delegateFlags.delegateSupportsNotificationAfterDrawing = [_delegate respondsToSelector:@selector(attributedTextContentView:didDrawLayoutFrame:inContext:)];
_delegateFlags.delegateSupportsNotificationBeforeTextBoxDrawing = [_delegate respondsToSelector:@selector(attributedTextContentView:shouldDrawBackgroundForTextBlock:frame:context:forLayoutFrame:)];
if (!_delegateFlags.delegateSupportsCustomViewsForLinks && !_delegateFlags.delegateSupportsGenericCustomViews)
{
[self removeAllCustomViewsForLinks];
}
// we don't draw the images if imageViews are provided by the delegate method
// if you want images to be drawn even though you use custom views, set it back to YES after setting delegate
if (_delegateFlags.delegateSupportsGenericCustomViews || _delegateFlags.delegateSupportsCustomViewsForAttachments)
{
_shouldDrawImages = NO;
}
else
{
_shouldDrawImages = YES;
}
}
@synthesize layouter = _layouter;
@synthesize layoutFrame = _layoutFrame;
@synthesize attributedString = _attributedString;
@synthesize delegate = _delegate;
@synthesize edgeInsets = _edgeInsets;
@synthesize shouldDrawImages = _shouldDrawImages;
@synthesize shouldDrawLinks = _shouldDrawLinks;
@synthesize shouldLayoutCustomSubviews = _shouldLayoutCustomSubviews;
@synthesize layoutOffset = _layoutOffset;
@synthesize backgroundOffset = _backgroundOffset;
@synthesize customViews;
@synthesize customViewsForLinksIndex;
@synthesize customViewsForAttachmentsIndex;
@synthesize relayoutMask = _relayoutMask;
@end