Skip to content

Commit

Permalink
Newline character support and truncated line sizing improvement. (Tex…
Browse files Browse the repository at this point in the history
…tureGroup#1193)

* Newline character support and truncated line sizing improvement.

For purposes of truncating text, respect explicit newlines.
Don't size to smaller than truncated line width unless we have to.

* Update CHANGELOG.md
  • Loading branch information
wiseoldduck authored and mikezucc committed Nov 7, 2018
1 parent 8ff1daa commit 23832ce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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

0 comments on commit 23832ce

Please sign in to comment.