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

[ASTextNode2] Add improved support for all line-break modes in experimental text node. #1150

Merged
merged 3 commits into from
Oct 17, 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] 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