-
Notifications
You must be signed in to change notification settings - Fork 25.1k
TextLayout: take full width if text wrapped #47435
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,8 +347,33 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage | |
| NSTextContainer *textContainer = layoutManager.textContainers.firstObject; | ||
| [layoutManager ensureLayoutForTextContainer:textContainer]; | ||
|
|
||
| NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; | ||
| __block BOOL textDidWrap = NO; | ||
| [layoutManager | ||
| enumerateLineFragmentsForGlyphRange:glyphRange | ||
| usingBlock:^( | ||
| CGRect overallRect, | ||
| CGRect usedRect, | ||
| NSTextContainer *_Nonnull usedTextContainer, | ||
| NSRange lineGlyphRange, | ||
| BOOL *_Nonnull stop) { | ||
| NSRange range = [layoutManager characterRangeForGlyphRange:lineGlyphRange | ||
| actualGlyphRange:nil]; | ||
| NSUInteger lastCharacterIndex = range.location + range.length - 1; | ||
| BOOL endsWithNewLine = | ||
| [textStorage.string characterAtIndex:lastCharacterIndex] == '\n'; | ||
| if (!endsWithNewLine && textStorage.string.length > lastCharacterIndex + 1) { | ||
|
Contributor
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. Would this correctly detect wrapping for something like the below? I think we may be able to just count lines. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/CountLines.html#//apple_ref/doc/uid/20001810-CJBGBIBB
Contributor
Author
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. No, that's not considered text wrap. In this case we want the current behaviour (text width) otherwise we'd break case 2 from #47435 (comment) |
||
| textDidWrap = YES; | ||
| *stop = YES; | ||
| } | ||
| }]; | ||
|
|
||
| CGSize size = [layoutManager usedRectForTextContainer:textContainer].size; | ||
|
|
||
| if (textDidWrap) { | ||
| size.width = textContainer.size.width; | ||
|
Contributor
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. I think we may have similar case to the above, for measuring max-content, where we don't want to set to size. I think in this case, it looks like that might be represented as |
||
| } | ||
|
|
||
| size = (CGSize){RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height)}; | ||
|
|
||
| __block auto attachments = TextMeasurement::Attachments{}; | ||
|
|
||
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.
I think we may also reach this path when
widthYogaMeasureModeisYogaMeasureMode.UNDEFINED, in which case we are asking for max-content size, and thewidthisNaN.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.
I think on L604, we could replace:
with
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.
In this case, should we keep the calculated width as the max width of the lines?
We set calculatedWidth = width only if the text wraps due to overflow. If the user write 2 short lines we still use the width of the maximum line