Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASTextNode2 to ignore certain text alignments while calculating intrinsic size #1166

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 27 additions & 11 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize

_textContainer.size = constrainedSize;
[self _ensureTruncationText];


// If constrained width is max/inf, the text node is calculating its intrinsic size.
const BOOL calculatingIntrinsicSize = (_textContainer.size.width >= ASTextContainerMaxSize.width);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Texture ever use the isVerticalForm property? We should probably just delete it, but if not maybe check for it here.


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

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

- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString forIntrinsicSize:(BOOL)isForIntrinsicSize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method name implies the second parameter is a CGSize, to me something like isForIntrinsicSize would imply a BOOL.

{
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 such as "right" and "center" causes the text 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 +380,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 forIntrinsicSize:NO];

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