mirror of
https://github.com/HackPlan/AsyncDisplayKit.git
synced 2026-04-26 12:55:56 +08:00
Revert "[ASTextNode] Fix text node truncation (#1863)"
This reverts commit 6238e5edbd.
We will re-apply this change, but there are some early signs of performance impacts that need to be investigated.
This commit is contained in:
@@ -10,8 +10,6 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
typedef NSTextStorage *(^ASTextKitContextTextStorageCreationBlock)(NSAttributedString *attributedString);
|
||||
|
||||
/**
|
||||
A threadsafe container for the TextKit components that ASTextKit uses to lay out and truncate its text.
|
||||
|
||||
@@ -32,19 +30,10 @@ typedef NSTextStorage *(^ASTextKitContextTextStorageCreationBlock)(NSAttributedS
|
||||
constrainedSize:(CGSize)constrainedSize
|
||||
layoutManagerCreationBlock:(NSLayoutManager * (^)(void))layoutCreationBlock
|
||||
layoutManagerDelegate:(id<NSLayoutManagerDelegate>)layoutManagerDelegate
|
||||
textStorageCreationBlock:(ASTextKitContextTextStorageCreationBlock)textStorageCreationBlock;
|
||||
textStorageCreationBlock:(NSTextStorage * (^)(NSAttributedString *attributedString))textStorageCreationBlock;
|
||||
|
||||
/**
|
||||
Set the constrained size for the text context.
|
||||
*/
|
||||
@property (nonatomic, assign, readwrite) CGSize constrainedSize;
|
||||
|
||||
/**
|
||||
Resets the text storage to the original value in case it was truncated before. This method is called within
|
||||
a locked context.
|
||||
*/
|
||||
- (void)resetTextStorage;
|
||||
|
||||
/**
|
||||
All operations on TextKit values MUST occur within this locked context. Simultaneous access (even non-mutative) to
|
||||
TextKit components may cause crashes.
|
||||
|
||||
@@ -21,12 +21,8 @@
|
||||
NSLayoutManager *_layoutManager;
|
||||
NSTextStorage *_textStorage;
|
||||
NSTextContainer *_textContainer;
|
||||
|
||||
NSAttributedString *_attributedString;
|
||||
}
|
||||
|
||||
#pragma mark - Lifecycle
|
||||
|
||||
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
|
||||
lineBreakMode:(NSLineBreakMode)lineBreakMode
|
||||
maximumNumberOfLines:(NSUInteger)maximumNumberOfLines
|
||||
@@ -41,23 +37,16 @@
|
||||
// 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 = [[NSTextStorage alloc] init];
|
||||
[self _resetTextStorage];
|
||||
_textStorage = (attributedString ? [[NSTextStorage alloc] initWithAttributedString:attributedString] : [[NSTextStorage alloc] init]);
|
||||
}
|
||||
|
||||
|
||||
_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;
|
||||
@@ -69,21 +58,6 @@
|
||||
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);
|
||||
@@ -96,8 +70,6 @@
|
||||
_textContainer.size = constrainedSize;
|
||||
}
|
||||
|
||||
#pragma mark - Locking
|
||||
|
||||
- (void)performBlockWithLockedTextKitComponents:(void (^)(NSLayoutManager *,
|
||||
NSTextStorage *,
|
||||
NSTextContainer *))block
|
||||
|
||||
@@ -128,12 +128,8 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
// If we're updating an existing context, make sure to use the same inset logic used during initialization.
|
||||
// This codepath allows us to reuse the
|
||||
CGSize shadowConstrainedSize = [[self shadower] insetSizeWithConstrainedSize:constrainedSize];
|
||||
if (_context) {
|
||||
_context.constrainedSize = shadowConstrainedSize;
|
||||
}
|
||||
if (_fontSizeAdjuster) {
|
||||
_fontSizeAdjuster.constrainedSize = shadowConstrainedSize;
|
||||
}
|
||||
if (_context) _context.constrainedSize = shadowConstrainedSize;
|
||||
if (_fontSizeAdjuster) _fontSizeAdjuster.constrainedSize = shadowConstrainedSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,14 +150,9 @@
|
||||
|
||||
- (void)truncate
|
||||
{
|
||||
// Reset the text storage to start always with the full string
|
||||
[_context resetTextStorage];
|
||||
|
||||
// Start truncation
|
||||
[_context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
|
||||
|
||||
NSUInteger originalStringLength = textStorage.length;
|
||||
|
||||
|
||||
[layoutManager ensureLayoutForTextContainer:textContainer];
|
||||
|
||||
NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:{ .size = textContainer.size }
|
||||
|
||||
@@ -125,7 +125,7 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
|
||||
for (NSInteger i = 10; i < 500; i += 50) {
|
||||
CGSize constrainedSize = CGSizeMake(i, i);
|
||||
CGSize calculatedSize = [_textNode measure:constrainedSize];
|
||||
CGSize recalculatedSize = [_textNode measure:constrainedSize];
|
||||
CGSize recalculatedSize = [_textNode measure:calculatedSize];
|
||||
|
||||
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
|
||||
}
|
||||
@@ -136,7 +136,7 @@ static BOOL CGSizeEqualToSizeWithIn(CGSize size1, CGSize size2, CGFloat delta)
|
||||
for (CGFloat i = 10; i < 500; i *= 1.3) {
|
||||
CGSize constrainedSize = CGSizeMake(i, i);
|
||||
CGSize calculatedSize = [_textNode measure:constrainedSize];
|
||||
CGSize recalculatedSize = [_textNode measure:constrainedSize];
|
||||
CGSize recalculatedSize = [_textNode measure:calculatedSize];
|
||||
|
||||
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user