Fix ASTextNode truncated size calculation ignoring attributes in the last line

This commit is contained in:
Samuel Hsiung
2015-12-15 10:09:03 -08:00
parent 0cc229d38d
commit f9d476e170
4 changed files with 24 additions and 3 deletions

Binary file not shown.

View File

@@ -305,6 +305,11 @@ static NSString *ASTextNodeTruncationTokenAttributeName = @"ASTextNodeTruncation
_attributedString = ASCleanseAttributedStringOfCoreTextAttributes(attributedString);
// Sync the truncation string with attributes from the updated _attributedString
// Without this, the size calculation of the text with truncation applied will
// not take into account the attributes of attributedString in the last line
[self _updateComposedTruncationString];
// We need an entirely new renderer
[self _invalidateRenderer];
@@ -1024,9 +1029,14 @@ static NSAttributedString *DefaultTruncationAttributedString()
#pragma mark - Truncation Message
- (void)_invalidateTruncationString
- (void)_updateComposedTruncationString
{
_composedTruncationString = [self _prepareTruncationStringForDrawing:[self _composedTruncationString]];
}
- (void)_invalidateTruncationString
{
[self _updateComposedTruncationString];
[self _invalidateRenderer];
ASDisplayNodeRespectThreadAffinityOfNode(self, ^{
[self setNeedsDisplay];

View File

@@ -62,6 +62,7 @@
effectiveRange:NULL];
NSParagraphStyle *paragraphStyle = [textStorage attributesAtIndex:[layoutManager characterIndexForGlyphAtIndex:lastVisibleGlyphIndex]
effectiveRange:NULL][NSParagraphStyleAttributeName];
// We assume LTR so long as the writing direction is not
BOOL rtlWritingDirection = paragraphStyle ? paragraphStyle.baseWritingDirection == NSWritingDirectionRightToLeft : NO;
// We only want to treat the trunction rect as left-aligned in the case that we are right-aligned and our writing

View File

@@ -54,7 +54,7 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
{
[super setUp];
_textNode = [[ASTextNode alloc] init];
UIFontDescriptor *desc =
[UIFontDescriptor fontDescriptorWithName:@"Didot" size:18];
NSArray *arr =
@@ -69,7 +69,17 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
[[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." attributes:d];
NSMutableParagraphStyle *para = [NSMutableParagraphStyle new];
para.alignment = NSTextAlignmentCenter;
[mas addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0,mas.length)];
para.lineSpacing = 1.0;
[mas addAttribute:NSParagraphStyleAttributeName value:para
range:NSMakeRange(0, mas.length - 1)];
// Vary the linespacing on the last line
NSMutableParagraphStyle *lastLinePara = [NSMutableParagraphStyle new];
lastLinePara.alignment = para.alignment;
lastLinePara.lineSpacing = 5.0;
[mas addAttribute:NSParagraphStyleAttributeName value:lastLinePara
range:NSMakeRange(mas.length - 1, 1)];
_attributedString = mas;
_textNode.attributedString = _attributedString;
}