diff --git a/CHANGELOG.md b/CHANGELOG.md index f0e4cd189..a5e88138b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ - Add NSLocking conformance to ASNodeController [Michael Schneider](https://github.com/maicki)[#1179] (https://github.com/TextureGroup/Texture/pull/1179) - Don’t handle touches on additional attributed message if passthrough is enabled [Michael Schneider](https://github.com/maicki)[#1184] (https://github.com/TextureGroup/Texture/pull/1184) - Yoga integration improvements [Michael Schneider](https://github.com/maicki)[#1187] (https://github.com/TextureGroup/Texture/pull/1187) +- Correct linePositionModifier behavior [Michael Schneider](https://github.com/maicki)[#1192] (https://github.com/TextureGroup/Texture/pull/1192) ## 2.7 - Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877) diff --git a/Source/ASTextNode.h b/Source/ASTextNode.h index b6c676a3a..1048017e7 100644 --- a/Source/ASTextNode.h +++ b/Source/ASTextNode.h @@ -217,6 +217,15 @@ NS_ASSUME_NONNULL_BEGIN @end +/** + * @abstract Text node unsupported properties + */ +@interface ASTextNode (Unsupported) + +@property (nullable, nonatomic) id textContainerLinePositionModifier; + +@end + /** * @abstract Text node deprecated properties */ diff --git a/Source/ASTextNode.mm b/Source/ASTextNode.mm index c6a8ca10a..ac39f8a5b 100644 --- a/Source/ASTextNode.mm +++ b/Source/ASTextNode.mm @@ -1389,6 +1389,21 @@ + (id)allocWithZone:(struct _NSZone *)zone @end +@implementation ASTextNode (Unsupported) + +- (void)setTextContainerLinePositionModifier:(id)textContainerLinePositionModifier +{ + AS_TEXT_ALERT_UNIMPLEMENTED_FEATURE(); +} + +- (id)textContainerLinePositionModifier +{ + AS_TEXT_ALERT_UNIMPLEMENTED_FEATURE(); + return nil; +} + +@end + @implementation ASTextNode (Deprecated) - (void)setAttributedString:(NSAttributedString *)attributedString diff --git a/Source/ASTextNode2.h b/Source/ASTextNode2.h index 771867251..442a47602 100644 --- a/Source/ASTextNode2.h +++ b/Source/ASTextNode2.h @@ -9,6 +9,8 @@ #import #import +@protocol ASTextLinePositionModifier; + NS_ASSUME_NONNULL_BEGIN /** @@ -207,6 +209,10 @@ NS_ASSUME_NONNULL_BEGIN + (void)enableDebugging; +#pragma mark - Layout and Sizing + +@property (nullable, nonatomic) id textContainerLinePositionModifier; + @end @interface ASTextNode2 (Unavailable) diff --git a/Source/ASTextNode2.mm b/Source/ASTextNode2.mm index e11d3b996..397bff6fa 100644 --- a/Source/ASTextNode2.mm +++ b/Source/ASTextNode2.mm @@ -318,6 +318,17 @@ - (UIEdgeInsets)textContainerInset return _textContainer.insets; } +- (void)setTextContainerLinePositionModifier:(id)modifier +{ + ASLockedSelfCompareAssignObjects(_textContainer.linePositionModifier, modifier); +} + +- (id)textContainerLinePositionModifier +{ + ASLockScopeSelf(); + return _textContainer.linePositionModifier; +} + - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize { ASDisplayNodeAssert(constrainedSize.width >= 0, @"Constrained width for text (%f) is too narrow", constrainedSize.width); diff --git a/Source/Private/TextExperiment/Component/ASTextLayout.m b/Source/Private/TextExperiment/Component/ASTextLayout.m index b1d9fee59..2612d9a69 100644 --- a/Source/Private/TextExperiment/Component/ASTextLayout.m +++ b/Source/Private/TextExperiment/Component/ASTextLayout.m @@ -102,6 +102,13 @@ @implementation ASTextContainer { id _linePositionModifier; } +- (NSString *)description +{ + return [NSString + stringWithFormat:@"immutable: %@, insets: %@, size: %@", self->_readonly ? @"YES" : @"NO", + NSStringFromUIEdgeInsets(self->_insets), NSStringFromCGSize(self->_size)]; +} + + (instancetype)containerWithSize:(CGSize)size NS_RETURNS_RETAINED { return [self containerWithSize:size insets:UIEdgeInsetsZero]; } @@ -373,6 +380,14 @@ - (instancetype)_init { return self; } +- (NSString *)description +{ + return [NSString stringWithFormat:@"lines: %ld, visibleRange:%@, textBoundingRect:%@", + [self.lines count], + NSStringFromRange(self.visibleRange), + NSStringFromCGRect(self.textBoundingRect)]; +} + + (ASTextLayout *)layoutWithContainerSize:(CGSize)size text:(NSAttributedString *)text { ASTextContainer *container = [ASTextContainer containerWithSize:size]; return [self layoutWithContainer:container text:text]; @@ -599,15 +614,24 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri position.y = cgPathBox.size.height + cgPathBox.origin.y - ctLineOrigin.y; ASTextLine *line = [ASTextLine lineWithCTLine:ctLine position:position vertical:isVerticalForm]; + + [lines addObject:line]; + } + + // Give user a chance to modify the line's position. + [container.linePositionModifier modifyLines:lines fromText:text inContainer:container]; + + NSUInteger i = 0; + for (ASTextLine *line in lines) { + CGPoint position = line.position; CGRect rect = line.bounds; - if (constraintSizeIsExtended) { if (isVerticalForm) { if (rect.origin.x + rect.size.width > constraintRectBeforeExtended.origin.x + constraintRectBeforeExtended.size.width) { measuringBeyondConstraints = YES; - }; + } } else { if (rect.origin.y + rect.size.height > constraintRectBeforeExtended.origin.y + @@ -640,11 +664,11 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri line.index = lineCurrentIdx; line.row = rowIdx; - [lines addObject:line]; + rowCount = rowIdx + 1; lineCurrentIdx ++; - if (i == 0) { + if (i++ == 0) { textBoundingRect = rect; } else if (!measuringBeyondConstraints) { if (maximumNumberOfRows == 0 || rowIdx < maximumNumberOfRows) { @@ -679,17 +703,6 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri } } - // Give user a chance to modify the line's position. - if (container.linePositionModifier) { - [container.linePositionModifier modifyLines:lines fromText:text inContainer:container]; - textBoundingRect = CGRectZero; - for (NSUInteger i = 0, max = lines.count; i < max; i++) { - ASTextLine *line = lines[i]; - if (i == 0) textBoundingRect = line.bounds; - else textBoundingRect = CGRectUnion(textBoundingRect, line.bounds); - } - } - lineRowsEdge = (ASRowEdge *) calloc(rowCount, sizeof(ASRowEdge)); if (lineRowsEdge == NULL) FAIL_AND_RETURN lineRowsIndex = (NSUInteger *) calloc(rowCount, sizeof(NSUInteger));