From 29ea23f38dbb250115937920835886d3b5b5e910 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Tue, 16 Oct 2018 15:42:10 -0700 Subject: [PATCH 1/2] 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. --- Source/ASTextNode2.mm | 5 ++++- .../TextExperiment/Component/ASTextLayout.m | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Source/ASTextNode2.mm b/Source/ASTextNode2.mm index f3d5487b8..967187fbe 100644 --- a/Source/ASTextNode2.mm +++ b/Source/ASTextNode2.mm @@ -246,7 +246,10 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize NSMutableAttributedString *mutableText = [_attributedText mutableCopy]; [self prepareAttributedString:mutableText isForIntrinsicSize:isCalculatingIntrinsicSize]; ASTextLayout *layout = [ASTextNode2 compatibleLayoutWithContainer:_textContainer text: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; } diff --git a/Source/Private/TextExperiment/Component/ASTextLayout.m b/Source/Private/TextExperiment/Component/ASTextLayout.m index b1d9fee59..e61889520 100644 --- a/Source/Private/TextExperiment/Component/ASTextLayout.m +++ b/Source/Private/TextExperiment/Component/ASTextLayout.m @@ -426,7 +426,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; @@ -820,23 +820,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); From 82af5734da6db4b7407843eea105780ee4c305d8 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 29 Oct 2018 10:26:32 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0e4cd189..a35607165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) - [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) - [ASExperimentalFeatures.m] Fix mismatch name in experimental features. [Max Wang](https://github.com/wsdwsd0829). [#1159](https://github.com/TextureGroup/Texture/pull/1159)