Skip to content

Commit e07a9f8

Browse files
committed
Merge branch 'release-1.5.2'
2 parents 5295943 + 1725516 commit e07a9f8

23 files changed

+569
-189
lines changed

Core/Source/DTAttributedLabel.m

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,33 @@ + (Class)layerClass
1818
return [CALayer class];
1919
}
2020

21+
- (void) setupAttributedLabel
22+
{
23+
// we want to relayout the text if height or width change
24+
self.relayoutMask = DTAttributedTextContentViewRelayoutOnHeightChanged | DTAttributedTextContentViewRelayoutOnWidthChanged;
25+
26+
self.layoutFrameHeightIsConstrainedByBounds = YES; // height is not flexible
27+
self.shouldAddFirstLineLeading = NO;
28+
}
29+
2130
- (id)initWithFrame:(CGRect)frame
2231
{
2332
self = [super initWithFrame:frame];
2433

2534
if (self)
2635
{
27-
// we want to relayout the text if height or width change
28-
self.relayoutMask = DTAttributedTextContentViewRelayoutOnHeightChanged | DTAttributedTextContentViewRelayoutOnWidthChanged;
29-
30-
self.layoutFrameHeightIsConstrainedByBounds = YES; // height is not flexible
31-
self.shouldAddFirstLineLeading = NO;
36+
[self setupAttributedLabel];
3237
}
3338

3439
return self;
3540
}
3641

42+
- (void) awakeFromNib
43+
{
44+
[super awakeFromNib];
45+
[self setupAttributedLabel];
46+
}
47+
3748
#pragma mark - Sizing
3849

3950
- (CGSize)intrinsicContentSize
@@ -44,7 +55,9 @@ - (CGSize)intrinsicContentSize
4455
}
4556

4657
// we have a layout frame and from this we get the needed size
47-
return [_layoutFrame intrinsicContentFrame].size;
58+
CGSize intrisicContentSize = [_layoutFrame intrinsicContentFrame].size;
59+
return CGSizeMake(intrisicContentSize.width + _edgeInsets.left + _edgeInsets.right,
60+
intrisicContentSize.height + _edgeInsets.top + _edgeInsets.bottom);
4861
}
4962

5063
#pragma mark - Properties

Core/Source/DTCoreTextConstants.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ extern NSString * const DTDefaultLineHeightMultiplier;
4949
extern NSString * const DTDefaultLineHeightMultiplier;
5050
extern NSString * const DTDefaultFirstLineHeadIndent;
5151
extern NSString * const DTDefaultHeadIndent;
52-
extern NSString * const DTDefaultListIndent;
5352
extern NSString * const DTDefaultStyleSheet;
5453
extern NSString * const DTUseiOS6Attributes;
5554
extern NSString * const DTWillFlushBlockCallBack;

Core/Source/DTCoreTextConstants.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
NSString * const DTDefaultLineHeightMultiplier = @"DTDefaultLineHeightMultiplier";
2323
NSString * const DTDefaultFirstLineHeadIndent = @"DTDefaultFirstLineHeadIndent";
2424
NSString * const DTDefaultHeadIndent = @"DTDefaultHeadIndent";
25-
NSString * const DTDefaultListIndent = @"DTDefaultListIndent";
2625
NSString * const DTDefaultStyleSheet = @"DTDefaultStyleSheet";
2726
NSString * const DTUseiOS6Attributes = @"DTUseiOS6Attributes";
2827
NSString * const DTWillFlushBlockCallBack = @"DTWillFlushBlockCallBack";

Core/Source/DTCoreTextFontDescriptor.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ - (id)initWithCTFontDescriptor:(CTFontDescriptorRef)ctFontDescriptor
220220

221221
- (id)initWithCTFont:(CTFontRef)ctFont
222222
{
223+
NSParameterAssert(ctFont);
224+
223225
self = [super init];
224226
if (self)
225227
{

Core/Source/DTCoreTextGlyphRun.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ - (void)drawDecorationInContext:(CGContextRef)context
158158

159159
if (backgroundColor)
160160
{
161+
CGRect backgroundColorRect = CGRectIntegral(CGRectMake(runStrokeBounds.origin.x, lineFrame.origin.y, runStrokeBounds.size.width, lineFrame.size.height));
162+
161163
CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
162-
CGContextFillRect(context, runStrokeBounds);
164+
CGContextFillRect(context, backgroundColorRect);
163165
}
164166

165167
if (drawStrikeOut || drawUnderline)

Core/Source/DTCoreTextLayoutFrame.m

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -540,17 +540,8 @@ - (void)_buildLinesWithTypesetter
540540
fittingLength += lineRange.length;
541541

542542
lineRange.location += lineRange.length;
543-
544-
// if there is a custom line height we need to adjust the ascender too
545-
if (usesForcedLineHeight)
546-
{
547-
// causes the line frame to encompass also the extra space
548-
newLine.ascent = lineHeight;
549-
}
550-
551543
previousLine = newLine;
552-
//previousLineMetrics = currentLineMetrics;
553-
}
544+
}
554545
while (lineRange.location < maxIndex && !truncateLine);
555546

556547
_lines = typesetLines;
@@ -756,6 +747,9 @@ - (CGRect)_frameForTextBlock:(DTTextBlock *)textBlock atIndex:(NSUInteger)locati
756747
{
757748
NSRange blockRange = [_attributedStringFragment rangeOfTextBlock:textBlock atIndex:location];
758749

750+
// need to reduce to actually visible string range in layout frame
751+
blockRange = NSIntersectionRange(blockRange, self.visibleStringRange);
752+
759753
DTCoreTextLayoutLine *firstBlockLine = [self lineContainingIndex:blockRange.location];
760754
DTCoreTextLayoutLine *lastBlockLine = [self lineContainingIndex:NSMaxRange(blockRange)-1];
761755

@@ -770,6 +764,13 @@ - (CGRect)_frameForTextBlock:(DTTextBlock *)textBlock atIndex:(NSUInteger)locati
770764
{
771765
DTCoreTextLayoutLine *oneLine = [self lineContainingIndex:index];
772766

767+
// avoid potential endless loop and log warning because this should never happen
768+
if (!oneLine)
769+
{
770+
NSLog(@"No line found containing index %ld of block range %@. This should never happen, please report that to [email protected]", (unsigned long)index, NSStringFromRange(blockRange));
771+
break;
772+
}
773+
773774
if (maxWidth<oneLine.frame.size.width)
774775
{
775776
maxWidth = oneLine.frame.size.width;

Core/Source/DTCoreTextParagraphStyle.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ The distance in points from the margin of a text container to the end of lines.
196196
@property (nonatomic, copy) NSArray *textLists;
197197

198198

199-
/**
200-
The amount by which each list level is indented from the previous. NOTE: about to be replaced by textLists property.
201-
*/
202-
@property (nonatomic, assign) CGFloat listIndent;
203-
204-
205199
/**-------------------------------------------------------------------------------------
206200
@name Setting Text Blocks
207201
---------------------------------------------------------------------------------------

Core/Source/DTCoreTextParagraphStyle.m

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
CGFloat paragraphSpacing;
2626
CGFloat headIndent;
2727
CGFloat tailIndent;
28-
CGFloat listIndent;
2928
CGFloat lineHeightMultiple;
3029
CGFloat minimumLineHeight;
3130
CGFloat maximumLineHeight;
@@ -42,7 +41,6 @@ @implementation DTCoreTextParagraphStyle
4241
CGFloat _paragraphSpacing;
4342
CGFloat _headIndent;
4443
CGFloat _tailIndent;
45-
CGFloat _listIndent;
4644
CGFloat _lineHeightMultiple;
4745
CGFloat _minimumLineHeight;
4846
CGFloat _maximumLineHeight;
@@ -132,7 +130,6 @@ - (id)init
132130
_minimumLineHeight = 0.0;
133131
_maximumLineHeight = 0.0;
134132
_paragraphSpacing = 0.0;
135-
_listIndent = 0;
136133
}
137134

138135
return self;
@@ -227,7 +224,7 @@ - (id)initWithCTParagraphStyle:(CTParagraphStyleRef)ctParagraphStyle
227224
allvalues_t *allvalues = &allvalues_stack; // pointer so that we can use the arrow operator
228225
#endif
229226

230-
*allvalues = (allvalues_t){0,0,0,0,0,0,0,0,0,0,0,0,0};
227+
*allvalues = (allvalues_t){0,0,0,0,0,0,0,0,0,0,0,0};
231228

232229
// pack all values in the struct
233230
allvalues->firstLineHeadIndent = _firstLineHeadIndent;
@@ -236,7 +233,6 @@ - (id)initWithCTParagraphStyle:(CTParagraphStyleRef)ctParagraphStyle
236233
allvalues->paragraphSpacing = _paragraphSpacing;
237234
allvalues->headIndent = _headIndent;
238235
allvalues->tailIndent = _tailIndent;
239-
allvalues->listIndent = _listIndent;
240236
allvalues->lineHeightMultiple = _lineHeightMultiple;
241237
allvalues->minimumLineHeight = _minimumLineHeight;
242238
allvalues->maximumLineHeight = _maximumLineHeight;
@@ -324,8 +320,6 @@ - (NSParagraphStyle *)NSParagraphStyle
324320
[mps setHeadIndent:_headIndent];
325321
[mps setTailIndent:_tailIndent];
326322

327-
// _listIndent not supported
328-
329323
[mps setMinimumLineHeight:_minimumLineHeight];
330324
[mps setMaximumLineHeight:_maximumLineHeight];
331325

@@ -501,7 +495,6 @@ - (id)copyWithZone:(NSZone *)zone
501495
newObject.minimumLineHeight = self.minimumLineHeight;
502496
newObject.maximumLineHeight = self.maximumLineHeight;
503497
newObject.headIndent = self.headIndent;
504-
newObject.listIndent = self.listIndent;
505498
newObject.alignment = self.alignment;
506499
newObject.baseWritingDirection = self.baseWritingDirection;
507500
newObject.tabStops = self.tabStops; // copy
@@ -635,7 +628,6 @@ - (void)setBaseWritingDirection:(CTWritingDirection)baseWritingDirection
635628
@synthesize maximumLineHeight = _maximumLineHeight;
636629
@synthesize headIndent = _headIndent;
637630
@synthesize tailIndent = _tailIndent;
638-
@synthesize listIndent = _listIndent;
639631
@synthesize alignment = _alignment;
640632
@synthesize textLists = _textLists;
641633
@synthesize textBlocks = _textBlocks;

Core/Source/DTHTMLAttributedStringBuilder.m

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ @implementation DTHTMLAttributedStringBuilder
6363
DTHTMLElement *_rootNode;
6464
DTHTMLElement *_bodyElement;
6565
DTHTMLElement *_currentTag;
66+
BOOL _ignoreParseEvents; // ignores events from parser after first HTML tag was finished
6667
}
6768

6869
- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)docAttributes
@@ -75,7 +76,7 @@ - (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttrib
7576

7677
// documentAttributes ignored for now
7778

78-
//GCD setup
79+
// GCD setup
7980
_stringAssemblyQueue = dispatch_queue_create("DTHTMLAttributedStringBuilder", 0);
8081
_stringAssemblyGroup = dispatch_group_create();
8182
_dataParsingQueue = dispatch_queue_create("DTHTMLAttributedStringBuilderParser", 0);
@@ -273,12 +274,6 @@ - (BOOL)_buildString
273274
_defaultParagraphStyle.headIndent = [defaultHeadIndent integerValue];
274275
}
275276

276-
NSNumber *defaultListIndent = [_options objectForKey:DTDefaultListIndent];
277-
if (defaultListIndent)
278-
{
279-
_defaultParagraphStyle.listIndent = [defaultListIndent integerValue];
280-
}
281-
282277
_defaultTag = [[DTHTMLElement alloc] init];
283278
_defaultTag.fontDescriptor = _defaultFontDescriptor;
284279
_defaultTag.paragraphStyle = _defaultParagraphStyle;
@@ -402,30 +397,28 @@ - (void)_registerTagStartHandlers
402397
[_tagStartHandlers setObject:[aBlock copy] forKey:@"a"];
403398

404399

405-
void (^liBlock)(void) = ^
406-
{
407-
_currentTag.paragraphStyle.paragraphSpacing = 0;
408-
409-
DTCSSListStyle *listStyle = [_currentTag.paragraphStyle.textLists lastObject];
410-
411-
if (listStyle.type != DTCSSListStyleTypeNone)
412-
{
413-
// first tab is to right-align bullet, numbering against
414-
CGFloat tabOffset = _currentTag.paragraphStyle.headIndent - 5.0f*_textScale;
415-
[_currentTag.paragraphStyle addTabStopAtPosition:tabOffset alignment:kCTRightTextAlignment];
416-
}
417-
418-
// second tab is for the beginning of first line after bullet
419-
[_currentTag.paragraphStyle addTabStopAtPosition:_currentTag.paragraphStyle.headIndent alignment: kCTLeftTextAlignment];
420-
};
421-
422-
[_tagStartHandlers setObject:[liBlock copy] forKey:@"li"];
400+
// void (^liBlock)(void) = ^
401+
// {
402+
// DTCSSListStyle *listStyle = [_currentTag.paragraphStyle.textLists lastObject];
403+
//
404+
// if (listStyle.type != DTCSSListStyleTypeNone)
405+
// {
406+
// // first tab is to right-align bullet, numbering against
407+
// CGFloat tabOffset = _currentTag.paragraphStyle.headIndent - 5.0f*_textScale;
408+
// [_currentTag.paragraphStyle addTabStopAtPosition:tabOffset alignment:kCTRightTextAlignment];
409+
// }
410+
//
411+
// // second tab is for the beginning of first line after bullet
412+
// [_currentTag.paragraphStyle addTabStopAtPosition:_currentTag.paragraphStyle.headIndent alignment: kCTLeftTextAlignment];
413+
// };
414+
//
415+
// [_tagStartHandlers setObject:[liBlock copy] forKey:@"li"];
423416

424417

425418
void (^listBlock)(void) = ^
426419
{
427420
_currentTag.paragraphStyle.firstLineHeadIndent = _currentTag.paragraphStyle.headIndent;
428-
_currentTag.paragraphStyle.headIndent += _currentTag.paragraphStyle.listIndent;
421+
// _currentTag.paragraphStyle.headIndent += _currentTag.paragraphStyle.listIndent;
429422

430423
// create the appropriate list style from CSS
431424
DTCSSListStyle *newListStyle = [_currentTag listStyle];
@@ -646,9 +639,15 @@ - (void)_registerTagEndHandlers
646639

647640
- (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName attributes:(NSDictionary *)attributeDict
648641
{
642+
649643
dispatch_group_async(_treeBuildingGroup, _treeBuildingQueue, ^{
650-
DTHTMLElement *newNode = [DTHTMLElement elementWithName:elementName attributes:attributeDict options:_options];
651644

645+
if (_ignoreParseEvents)
646+
{
647+
return;
648+
}
649+
650+
DTHTMLElement *newNode = [DTHTMLElement elementWithName:elementName attributes:attributeDict options:_options];
652651
DTHTMLElement *previousLastChild = nil;
653652

654653
if (_currentTag)
@@ -670,6 +669,8 @@ - (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName at
670669
}
671670
else
672671
{
672+
NSAssert(!_rootNode, @"Something went wrong, second root node found in document and not ignored.");
673+
673674
// might be first node ever
674675
if (!_rootNode)
675676
{
@@ -716,7 +717,14 @@ - (void)parser:(DTHTMLParser *)parser didStartElement:(NSString *)elementName at
716717

717718
- (void)parser:(DTHTMLParser *)parser didEndElement:(NSString *)elementName
718719
{
720+
719721
dispatch_group_async(_treeBuildingGroup, _treeBuildingQueue, ^{
722+
723+
if (_ignoreParseEvents)
724+
{
725+
return;
726+
}
727+
720728
// output the element if it is direct descendant of body tag, or close of body in case there are direct text nodes
721729

722730
// find block to execute for this tag if any
@@ -783,6 +791,12 @@ - (void)parser:(DTHTMLParser *)parser didEndElement:(NSString *)elementName
783791
// missing end of element, attempt to recover
784792
_currentTag = [_currentTag parentElement];
785793
}
794+
795+
// closing the root node, ignore everything afterwards
796+
if (_currentTag == _rootNode)
797+
{
798+
_ignoreParseEvents = YES;
799+
}
786800

787801
// go back up a level
788802
_currentTag = [_currentTag parentElement];
@@ -795,6 +809,11 @@ - (void)parser:(DTHTMLParser *)parser foundCharacters:(NSString *)string
795809
dispatch_group_async(_treeBuildingGroup, _treeBuildingQueue, ^{
796810
NSAssert(_currentTag, @"Cannot add text node without a current node");
797811

812+
if (_ignoreParseEvents)
813+
{
814+
return;
815+
}
816+
798817
if ([string isIgnorableWhitespace])
799818
{
800819
// ignore whitespace as first element of block element
@@ -853,6 +872,12 @@ - (void)parser:(DTHTMLParser *)parser foundCharacters:(NSString *)string
853872
- (void)parser:(DTHTMLParser *)parser foundCDATA:(NSData *)CDATABlock
854873
{
855874
dispatch_group_async(_treeBuildingGroup, _treeBuildingQueue, ^{
875+
876+
if (_ignoreParseEvents)
877+
{
878+
return;
879+
}
880+
856881
NSAssert(_currentTag, @"Cannot add text node without a current node");
857882

858883
NSString *styleBlock = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
@@ -866,9 +891,10 @@ - (void)parser:(DTHTMLParser *)parser foundCDATA:(NSData *)CDATABlock
866891

867892
- (void)parserDidEndDocument:(DTHTMLParser *)parser
868893
{
894+
869895
dispatch_group_async(_treeBuildingGroup, _treeBuildingQueue, ^{
870896
NSAssert(!_currentTag, @"Something went wrong, at end of document there is still an open node");
871-
897+
872898
dispatch_group_async(_stringAssemblyGroup, _stringAssemblyQueue, ^{
873899
// trim off white space at end
874900
while ([[_tmpString string] hasSuffixCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]])

Core/Source/DTHTMLElement.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
@interface DTHTMLElement : DTHTMLParserNode
2525
{
26-
DTHTMLElement *_parent;
27-
2826
DTCoreTextFontDescriptor *_fontDescriptor;
2927
DTCoreTextParagraphStyle *_paragraphStyle;
3028
DTTextAttachment *_textAttachment;
@@ -74,6 +72,9 @@
7472
// margins/padding
7573
DTEdgeInsets _margins;
7674
DTEdgeInsets _padding;
75+
76+
// indent of lists
77+
CGFloat _listIndent;
7778
}
7879

7980
/**

0 commit comments

Comments
 (0)