Skip to content

Commit

Permalink
ASTextNode2 to ignore certain text alignments while calculating intri…
Browse files Browse the repository at this point in the history
…nsic size (TextureGroup#1166)

ASTextNode2 uses ASTextLayout to calculate its layout and bounding rect. When the constrained width that is used for layout calculation is inf/max (e.g when the node is inside a horizontal stack), ASTextLayout doesn't ignore its right/center/natural text alignment but takes it into account. That results in an unreasonable size (and position).

Fix by detecting when the node is calculating intrinsic size and force its layout alignment to be left. Other alignments should still work when the max width is finite/reasonable.
  • Loading branch information
nguyenhuy authored and mikezucc committed Nov 7, 2018
1 parent 77edb0f commit 8546cbd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- [ASCollectionView] Fix a crash that is caused by clearing a collection view's data while it's still being used. [Huy Nguyen](https://github.com/nguyenhuy) [#1154](https://github.com/TextureGroup/Texture/pull/1154)
- Clean up timing of layout tree flattening/ copying of unflattened tree for Weaver. [Michael Zuccarino](https://github.com/mikezucc) [#1157](https://github.com/TextureGroup/Texture/pull/1157)
- Fix crash setting attributed text on multiple threads [Michael Schneider](https://github.com/maicki)
- [ASTextNode2] Ignore certain text alignments while calculating intrinsic size [Huy Nguyen](https://github.com/nguyenhuy) [#1166](https://github.com/TextureGroup/Texture/pull/1166)

## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
43 changes: 32 additions & 11 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,17 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize

_textContainer.size = constrainedSize;
[self _ensureTruncationText];


// If the constrained size has a max/inf value on the text's forward direction, the text node is calculating its intrinsic size.
BOOL isCalculatingIntrinsicSize;
if (_textContainer.isVerticalForm) {
isCalculatingIntrinsicSize = (_textContainer.size.height >= ASTextContainerMaxSize.height);
} else {
isCalculatingIntrinsicSize = (_textContainer.size.width >= ASTextContainerMaxSize.width);
}

NSMutableAttributedString *mutableText = [_attributedText mutableCopy];
[self prepareAttributedString:mutableText];
[self prepareAttributedString:mutableText isForIntrinsicSize:isCalculatingIntrinsicSize];
ASTextLayout *layout = [ASTextNode2 compatibleLayoutWithContainer:_textContainer text:mutableText];

return layout.textBoundingSize;
Expand Down Expand Up @@ -321,18 +329,31 @@ - (NSArray *)exclusionPaths
return _textContainer.exclusionPaths;
}

- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString isForIntrinsicSize:(BOOL)isForIntrinsicSize
{
ASLockScopeSelf();
// Apply paragraph style if needed

// Apply/Fix paragraph style if needed
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:kNilOptions usingBlock:^(NSParagraphStyle *style, NSRange range, BOOL * _Nonnull stop) {
if (style == nil || style.lineBreakMode == _truncationMode) {

const BOOL applyTruncationMode = (style != nil && style.lineBreakMode != _truncationMode);
// Only "left" and "justified" alignments are supported while calculating intrinsic size.
// Other alignments like "right", "center" and "natural" cause the size to be bigger than needed and thus should be ignored/overridden.
const BOOL forceLeftAlignment = (style != nil
&& isForIntrinsicSize
&& style.alignment != NSTextAlignmentLeft
&& style.alignment != NSTextAlignmentJustified);
if (!applyTruncationMode && !forceLeftAlignment) {
return;
}

NSMutableParagraphStyle *paragraphStyle = [style mutableCopy] ?: [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = _truncationMode;

NSMutableParagraphStyle *paragraphStyle = [style mutableCopy];
if (applyTruncationMode) {
paragraphStyle.lineBreakMode = _truncationMode;
}
if (forceLeftAlignment) {
paragraphStyle.alignment = NSTextAlignmentLeft;
}
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
}];

Expand Down Expand Up @@ -364,8 +385,8 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
copiedContainer.size = self.bounds.size;
[copiedContainer makeImmutable];
NSMutableAttributedString *mutableText = [_attributedText mutableCopy] ?: [[NSMutableAttributedString alloc] init];
[self prepareAttributedString:mutableText];

[self prepareAttributedString:mutableText isForIntrinsicSize:NO];

return @{
@"container": copiedContainer,
Expand Down

0 comments on commit 8546cbd

Please sign in to comment.