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

Newline character support and truncated line sizing improvement. #1193

Merged
merged 4 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,5 +1,6 @@
## master
* Add your own contributions to the next release on the line below this with your name.
- [ASTextNode2] Newline character support and truncated line sizing improvement. [Kevin Smith](https://github.com/wiseoldduck). [#1193](https://github.com/TextureGroup/Texture/pull/1193)
- [ASScrollNode] A11y support for ASScrollNode. [Max Wang](https://github.com/wsdwsd0829). [#1188](https://github.com/TextureGroup/Texture/pull/1188)
- [ASDisplayNode.m] Make sure node is loaded before enter visible. [Max Wang](https://github.com/wsdwsd0829). [#886](https://github.com/TextureGroup/Texture/pull/886)
- [ASTextNode2] Add improved support for all line-break modes in experimental text node. [Kevin Smith](https://github.com/wiseoldduck). [#1150](https://github.com/TextureGroup/Texture/pull/1150)
Expand Down
5 changes: 4 additions & 1 deletion Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
NSMutableAttributedString *mutableText = [_attributedText mutableCopy];
[self prepareAttributedString:mutableText isForIntrinsicSize:isCalculatingIntrinsicSize];
ASTextLayout *layout = ASTextNodeCompatibleLayoutWithContainerAndText(_textContainer, mutableText);

if (layout.truncatedLine != nil && layout.truncatedLine.size.width > layout.textBoundingSize.width) {
return (CGSize) {MIN(constrainedSize.width, layout.truncatedLine.size.width), layout.textBoundingSize.height};
}

return layout.textBoundingSize;
}

Expand Down
18 changes: 14 additions & 4 deletions Source/Private/TextExperiment/Component/ASTextLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri
// It may use larger constraint size when create CTFrame with
// CTFramesetterCreateFrame in iOS 10.
BOOL needFixLayoutSizeBug = AS_AT_LEAST_IOS10;

layout = [[ASTextLayout alloc] _init];
layout.text = text;
layout.container = container;
Expand Down Expand Up @@ -834,23 +834,33 @@ + (ASTextLayout *)layoutWithContainer:(ASTextContainer *)container text:(NSAttri
}
}
int i = 0;
if (type != kCTLineTruncationStart) { // Middle or End/Tail wants text preceding truncated content
if (type != kCTLineTruncationStart) { // Middle or End/Tail wants text preceding truncated content.
i = removedLines.count - 1;
while (atLeastOneLine < truncatedWidth && i >= 0) {
if (lastLineText.length > 0 && [lastLineText.string characterAtIndex:lastLineText.string.length - 1] == '\n') { // Explicit newlines are always "long enough".
[lastLineText deleteCharactersInRange:NSMakeRange(lastLineText.string.length - 1, 1)];
break;
}
[lastLineText appendAttributedString:[text attributedSubstringFromRange:removedLines[i].range]];
atLeastOneLine += removedLines[i--].width;
}
[lastLineText appendAttributedString:truncationToken];
}
if (type != kCTLineTruncationEnd && removedLines.count > 0) { // Middle or Start/Head wants text following truncated content
if (type != kCTLineTruncationEnd && removedLines.count > 0) { // Middle or Start/Head wants text following truncated content.
i = 0;
atLeastOneLine = removedLines[i].width;
while (atLeastOneLine < truncatedWidth && i < removedLines.count) {
atLeastOneLine += removedLines[i++].width;
}
for (i--; i >= 0; i--) {
[lastLineText appendAttributedString:[text attributedSubstringFromRange:removedLines[i].range]];
NSAttributedString *nextLine = [text attributedSubstringFromRange:removedLines[i].range];
if ([nextLine.string characterAtIndex:nextLine.string.length - 1] == '\n') { // Explicit newlines are always "long enough".
lastLineText = [NSMutableAttributedString new];
} else {
[lastLineText appendAttributedString:nextLine];
}
}
[lastLineText insertAttributedString:truncationToken atIndex:0];
}

CTLineRef ctLastLineExtend = CTLineCreateWithAttributedString((CFAttributedStringRef) lastLineText);
Expand Down