[ASTextNode] Fix text node truncation (#1863)

* Before truncate a text storage in ASTextKitContext reset the text storage to original value

* Fix ASTextNode tests

We should pass in the constrained size in both cases and the sizes should be the same. We adjust the calculated size in ASTextNode to be a bit narrower in the second case if we truncate again with the calculated size as constrained size it will truncate more and the resulting size will shrink.
This commit is contained in:
Michael Schneider
2016-07-09 15:40:31 -07:00
committed by appleguy
parent 38fab7cd94
commit 6238e5edbd
5 changed files with 56 additions and 8 deletions

View File

@@ -21,8 +21,12 @@
NSLayoutManager *_layoutManager;
NSTextStorage *_textStorage;
NSTextContainer *_textContainer;
NSAttributedString *_attributedString;
}
#pragma mark - Lifecycle
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
lineBreakMode:(NSLineBreakMode)lineBreakMode
maximumNumberOfLines:(NSUInteger)maximumNumberOfLines
@@ -37,16 +41,23 @@
// Concurrently initialising TextKit components crashes (rdar://18448377) so we use a global lock.
static std::mutex __static_mutex;
std::lock_guard<std::mutex> l(__static_mutex);
_attributedString = [attributedString copy];
// Create the TextKit component stack with our default configuration.
if (textStorageCreationBlock) {
_textStorage = textStorageCreationBlock(attributedString);
} else {
_textStorage = (attributedString ? [[NSTextStorage alloc] initWithAttributedString:attributedString] : [[NSTextStorage alloc] init]);
_textStorage = [[NSTextStorage alloc] init];
[self _resetTextStorage];
}
_layoutManager = layoutCreationBlock ? layoutCreationBlock() : [[ASLayoutManager alloc] init];
_layoutManager.usesFontLeading = NO;
_layoutManager.delegate = layoutManagerDelegate;
[_textStorage addLayoutManager:_layoutManager];
_textContainer = [[NSTextContainer alloc] initWithSize:constrainedSize];
// We want the text laid out up to the very edges of the container.
_textContainer.lineFragmentPadding = 0;
@@ -58,6 +69,21 @@
return self;
}
#pragma mark - Text Storage
- (void)resetTextStorage
{
std::lock_guard<std::mutex> l(_textKitMutex);
[self _resetTextStorage];
}
- (void)_resetTextStorage
{
[_textStorage setAttributedString:_attributedString];
}
#pragma mark - Setter / Getter
- (CGSize)constrainedSize
{
std::lock_guard<std::mutex> l(_textKitMutex);
@@ -70,6 +96,8 @@
_textContainer.size = constrainedSize;
}
#pragma mark - Locking
- (void)performBlockWithLockedTextKitComponents:(void (^)(NSLayoutManager *,
NSTextStorage *,
NSTextContainer *))block