-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
nguyenhuy
merged 4 commits into
TextureGroup:master
from
nguyenhuy:HN-ASTextNode2-Alignment-Inf-Width
Oct 11, 2018
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
NSMutableAttributedString *mutableText = [_attributedText mutableCopy]; | ||
[self prepareAttributedString:mutableText]; | ||
[self prepareAttributedString:mutableText forIntrinsicSize:calculatingIntrinsicSize]; | ||
ASTextLayout *layout = [ASTextNode2 compatibleLayoutWithContainer:_textContainer text:mutableText]; | ||
|
||
return layout.textBoundingSize; | ||
|
@@ -321,18 +324,31 @@ - (NSArray *)exclusionPaths | |
return _textContainer.exclusionPaths; | ||
} | ||
|
||
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString | ||
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString forIntrinsicSize:(BOOL)isForIntrinsicSize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method name implies the second parameter is a |
||
{ | ||
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]; | ||
}]; | ||
|
||
|
@@ -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, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.