mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-09-14 08:03:59 +00:00
Add ability to customize NSLayoutManager and NSTextStorage when created in the ASTextKitContext
This commit is contained in:
parent
896f0adcaf
commit
00b0968bf7
@ -213,6 +213,16 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) {
|
||||
*/
|
||||
@property (nonatomic, assign) BOOL passthroughNonlinkTouches;
|
||||
|
||||
#pragma mark - ASTextKit Customization
|
||||
/**
|
||||
A block to provide a hook to provide a custom NSLayoutManager to the ASTextKitRenderer
|
||||
*/
|
||||
@property (nonatomic, copy) NSLayoutManager * (^layoutManagerCreationBlock)(void);
|
||||
|
||||
/**
|
||||
A block to provide a hook to provide a NSTextStorage to the Text Kit's layout manager.
|
||||
*/
|
||||
@property (nonatomic, copy) NSTextStorage * (^textStorageCreationBlock)(NSAttributedString *attributedString);
|
||||
|
||||
@end
|
||||
|
||||
|
@ -245,6 +245,8 @@ static NSArray *DefaultLinkAttributeNames = @[ NSLinkAttributeName ];
|
||||
.exclusionPaths = _exclusionPaths,
|
||||
.pointSizeScaleFactors = _pointSizeScaleFactors,
|
||||
.currentScaleFactor = self.currentScaleFactor,
|
||||
.layoutManagerCreationBlock = self.layoutManagerCreationBlock,
|
||||
.textStorageCreationBlock = self.textStorageCreationBlock,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -91,15 +91,20 @@ struct ASTextKitAttributes {
|
||||
*/
|
||||
CGFloat currentScaleFactor;
|
||||
/**
|
||||
A pointer to a function that that returns a custom layout manager subclass. If nil, defaults to NSLayoutManager.
|
||||
An optional block that returns a custom layout manager subclass. If nil, defaults to NSLayoutManager.
|
||||
*/
|
||||
NSLayoutManager *(*layoutManagerFactory)(void);
|
||||
NSLayoutManager * (^layoutManagerCreationBlock)(void);
|
||||
|
||||
/**
|
||||
An optional delegate for the NSLayoutManager
|
||||
*/
|
||||
id<NSLayoutManagerDelegate> layoutManagerDelegate;
|
||||
|
||||
/**
|
||||
An optional block that returns a custom NSTextStorage for the layout manager.
|
||||
*/
|
||||
NSTextStorage * (^textStorageCreationBlock)(NSAttributedString *attributedString);
|
||||
|
||||
/**
|
||||
We provide an explicit copy function so we can use aggregate initializer syntax while providing copy semantics for
|
||||
the NSObjects inside.
|
||||
@ -119,8 +124,9 @@ struct ASTextKitAttributes {
|
||||
shadowRadius,
|
||||
pointSizeScaleFactors,
|
||||
currentScaleFactor,
|
||||
layoutManagerFactory,
|
||||
layoutManagerCreationBlock,
|
||||
layoutManagerDelegate,
|
||||
textStorageCreationBlock,
|
||||
};
|
||||
};
|
||||
|
||||
@ -133,7 +139,8 @@ struct ASTextKitAttributes {
|
||||
&& shadowRadius == other.shadowRadius
|
||||
&& [pointSizeScaleFactors isEqualToArray:other.pointSizeScaleFactors]
|
||||
&& currentScaleFactor == currentScaleFactor
|
||||
&& layoutManagerFactory == other.layoutManagerFactory
|
||||
&& layoutManagerCreationBlock == other.layoutManagerCreationBlock
|
||||
&& textStorageCreationBlock == other.textStorageCreationBlock
|
||||
&& CGSizeEqualToSize(shadowOffset, other.shadowOffset)
|
||||
&& _objectsEqual(exclusionPaths, other.exclusionPaths)
|
||||
&& _objectsEqual(avoidTailTruncationSet, other.avoidTailTruncationSet)
|
||||
|
@ -23,7 +23,8 @@ size_t ASTextKitAttributes::hash() const
|
||||
[attributedString hash],
|
||||
[truncationAttributedString hash],
|
||||
[avoidTailTruncationSet hash],
|
||||
std::hash<NSUInteger>()((NSUInteger) layoutManagerFactory),
|
||||
std::hash<NSUInteger>()((NSUInteger) layoutManagerCreationBlock),
|
||||
std::hash<NSUInteger>()((NSUInteger) textStorageCreationBlock),
|
||||
std::hash<NSInteger>()(lineBreakMode),
|
||||
std::hash<NSInteger>()(maximumNumberOfLines),
|
||||
[exclusionPaths hash],
|
||||
|
@ -28,8 +28,9 @@
|
||||
maximumNumberOfLines:(NSUInteger)maximumNumberOfLines
|
||||
exclusionPaths:(NSArray *)exclusionPaths
|
||||
constrainedSize:(CGSize)constrainedSize
|
||||
layoutManagerFactory:(NSLayoutManager*(*)(void))layoutManagerFactory
|
||||
layoutManagerDelegate:(id<NSLayoutManagerDelegate>)layoutManagerDelegate;
|
||||
layoutManagerCreationBlock:(NSLayoutManager * (^)(void))layoutCreationBlock
|
||||
layoutManagerDelegate:(id<NSLayoutManagerDelegate>)layoutManagerDelegate
|
||||
textStorageCreationBlock:(NSTextStorage * (^)(NSAttributedString *attributedString))textStorageCreationBlock;
|
||||
|
||||
@property (nonatomic, assign, readwrite) CGSize constrainedSize;
|
||||
|
||||
|
@ -29,16 +29,22 @@
|
||||
maximumNumberOfLines:(NSUInteger)maximumNumberOfLines
|
||||
exclusionPaths:(NSArray *)exclusionPaths
|
||||
constrainedSize:(CGSize)constrainedSize
|
||||
layoutManagerFactory:(NSLayoutManager*(*)(void))layoutManagerFactory
|
||||
layoutManagerCreationBlock:(NSLayoutManager * (^)(void))layoutCreationBlock
|
||||
layoutManagerDelegate:(id<NSLayoutManagerDelegate>)layoutManagerDelegate
|
||||
textStorageCreationBlock:(NSTextStorage * (^)(NSAttributedString *attributedString))textStorageCreationBlock
|
||||
|
||||
{
|
||||
if (self = [super init]) {
|
||||
// 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);
|
||||
// Create the TextKit component stack with our default configuration.
|
||||
if (textStorageCreationBlock) {
|
||||
_textStorage = textStorageCreationBlock(attributedString);
|
||||
} else {
|
||||
_textStorage = (attributedString ? [[NSTextStorage alloc] initWithAttributedString:attributedString] : [[NSTextStorage alloc] init]);
|
||||
_layoutManager = layoutManagerFactory ? layoutManagerFactory() : [[ASLayoutManager alloc] init];
|
||||
}
|
||||
_layoutManager = layoutCreationBlock ? layoutCreationBlock() : [[ASLayoutManager alloc] init];
|
||||
_layoutManager.usesFontLeading = NO;
|
||||
_layoutManager.delegate = layoutManagerDelegate;
|
||||
[_textStorage addLayoutManager:_layoutManager];
|
||||
|
@ -81,8 +81,8 @@
|
||||
static std::mutex __static_mutex;
|
||||
std::lock_guard<std::mutex> l(__static_mutex);
|
||||
|
||||
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
|
||||
NSLayoutManager *layoutManager = _attributes.layoutManagerFactory ? _attributes.layoutManagerFactory() : [[ASLayoutManager alloc] init];
|
||||
NSTextStorage *textStorage = _attributes.textStorageCreationBlock ? _attributes.textStorageCreationBlock(attributedString) : [[NSTextStorage alloc] initWithAttributedString:attributedString];
|
||||
NSLayoutManager *layoutManager = _attributes.layoutManagerCreationBlock ? _attributes.layoutManagerCreationBlock() : [[ASLayoutManager alloc] init];
|
||||
layoutManager.usesFontLeading = NO;
|
||||
[textStorage addLayoutManager:layoutManager];
|
||||
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:_constrainedSize];
|
||||
|
@ -104,8 +104,9 @@ static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
|
||||
maximumNumberOfLines:attributes.maximumNumberOfLines
|
||||
exclusionPaths:attributes.exclusionPaths
|
||||
constrainedSize:shadowConstrainedSize
|
||||
layoutManagerFactory:attributes.layoutManagerFactory
|
||||
layoutManagerDelegate:attributes.layoutManagerDelegate];
|
||||
layoutManagerCreationBlock:attributes.layoutManagerCreationBlock
|
||||
layoutManagerDelegate:attributes.layoutManagerDelegate
|
||||
textStorageCreationBlock:attributes.textStorageCreationBlock];
|
||||
}
|
||||
return _context;
|
||||
}
|
||||
|
@ -72,8 +72,9 @@
|
||||
maximumNumberOfLines:1
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedRect.size
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
__block CGRect truncationUsedRect;
|
||||
|
||||
[truncationContext performBlockWithLockedTextKitComponents:^(NSLayoutManager *truncationLayoutManager, NSTextStorage *truncationTextStorage, NSTextContainer *truncationTextContainer) {
|
||||
|
@ -42,8 +42,9 @@
|
||||
maximumNumberOfLines:0
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedSize
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
__block NSRange textKitVisibleRange;
|
||||
[context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
|
||||
textKitVisibleRange = [layoutManager characterRangeForGlyphRange:[layoutManager glyphRangeForTextContainer:textContainer]
|
||||
@ -64,8 +65,9 @@
|
||||
maximumNumberOfLines:0
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedSize
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
|
||||
truncationAttributedString:[self _simpleTruncationAttributedString]
|
||||
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
|
||||
@ -87,8 +89,9 @@
|
||||
maximumNumberOfLines:0
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedSize
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
|
||||
truncationAttributedString:[self _simpleTruncationAttributedString]
|
||||
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
|
||||
@ -111,8 +114,9 @@
|
||||
maximumNumberOfLines:0
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedSize
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
ASTextKitTailTruncater *tailTruncater = [[ASTextKitTailTruncater alloc] initWithContext:context
|
||||
truncationAttributedString:[self _simpleTruncationAttributedString]
|
||||
avoidTailTruncationSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
|
||||
@ -136,8 +140,9 @@
|
||||
maximumNumberOfLines:0
|
||||
exclusionPaths:nil
|
||||
constrainedSize:constrainedSize
|
||||
layoutManagerFactory:nil
|
||||
layoutManagerDelegate:nil];
|
||||
layoutManagerCreationBlock:nil
|
||||
layoutManagerDelegate:nil
|
||||
textStorageCreationBlock:nil];
|
||||
|
||||
XCTAssertNoThrow([[ASTextKitTailTruncater alloc] initWithContext:context
|
||||
truncationAttributedString:[self _simpleTruncationAttributedString]
|
||||
|
Loading…
x
Reference in New Issue
Block a user