Skip to content

Commit

Permalink
[ASTextNode2] Add improved support for all line-break modes in experi…
Browse files Browse the repository at this point in the history
…mental text node. (#1150)

* ASTextNode2 rendering corrections.

ASTextNode2 was only setting truncationMode (lineBreakMode) on existing paragraph styles in attributedString (thus having no effect for the two non-truncating modes if there were not any existing paragraph style runs).

ASTextLayout (essentially YYTextLayout) was not rendering the two non-tail truncation lineBreakModes correctly. There's not much history on github but it appears to me that it was set up correctly at one time and then some additional code was added for unclear reasons that assumed any truncation was at the end of the string.

This commit corrects both issues.

* Update CHANGELOG.md
  • Loading branch information
wiseoldduck authored and Adlai-Holler committed Oct 17, 2018
1 parent eab0fc2 commit bfb2298
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 160 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] 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)
- [ASCollectionViewLayoutController] Set default tuning parameters before view is loaded. [Max Wang](https://github.com/wsdwsd0829). [#1158](https://github.com/TextureGroup/Texture/pull/1158)
- [ASPhotosFrameworkImageRequestTests] Guard photo library with macro for tests. [Max Wang](https://github.com/wsdwsd0829). [#1147](https://github.com/TextureGroup/Texture/pull/1147)
Expand Down
28 changes: 25 additions & 3 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -332,22 +332,44 @@ - (NSArray *)exclusionPaths
- (void)prepareAttributedString:(NSMutableAttributedString *)attributedString isForIntrinsicSize:(BOOL)isForIntrinsicSize
{
ASLockScopeSelf();
NSLineBreakMode innerMode;
switch (_truncationMode) {
case NSLineBreakByWordWrapping:
case NSLineBreakByCharWrapping:
case NSLineBreakByClipping:
innerMode = _truncationMode;
break;
default:
innerMode = NSLineBreakByWordWrapping;
}

// Apply/Fix paragraph style if needed
[attributedString enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, attributedString.length) options:kNilOptions usingBlock:^(NSParagraphStyle *style, NSRange range, BOOL * _Nonnull stop) {

const BOOL applyTruncationMode = (style != nil && style.lineBreakMode != _truncationMode);
BOOL applyTruncationMode = YES;
NSMutableParagraphStyle *paragraphStyle = nil;
// Only "left" and "justified" alignments are supported while calculating intrinsic size.
// Other alignments like "right", "center" and "natural" cause the size to be bigger than needed and thus should be ignored/overridden.
const BOOL forceLeftAlignment = (style != nil
&& isForIntrinsicSize
&& style.alignment != NSTextAlignmentLeft
&& style.alignment != NSTextAlignmentJustified);
if (style != nil) {
if (innerMode == style.lineBreakMode) {
applyTruncationMode = NO;
}
paragraphStyle = [style mutableCopy];
} else {
if (innerMode == NSLineBreakByWordWrapping) {
applyTruncationMode = NO;
}
paragraphStyle = [NSMutableParagraphStyle new];
}
if (!applyTruncationMode && !forceLeftAlignment) {
return;
}
paragraphStyle.lineBreakMode = innerMode;

NSMutableParagraphStyle *paragraphStyle = [style mutableCopy];
if (applyTruncationMode) {
paragraphStyle.lineBreakMode = _truncationMode;
}
Expand All @@ -356,7 +378,7 @@ - (void)prepareAttributedString:(NSMutableAttributedString *)attributedString is
}
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
}];

// Apply shadow if needed
if (_shadowOpacity > 0 && (_shadowRadius != 0 || !CGSizeEqualToSize(_shadowOffset, CGSizeZero)) && CGColorGetAlpha(_shadowColor) > 0) {
NSShadow *shadow = [[NSShadow alloc] init];
Expand Down
Loading

0 comments on commit bfb2298

Please sign in to comment.