From f8f3585764f663087f52691317ce38d464c8de52 Mon Sep 17 00:00:00 2001 From: Eric Jensen Date: Fri, 25 Mar 2016 22:12:19 -0700 Subject: [PATCH] Add a ASTextKitComponents initializer that allows customization of its NSTextStorage and NSLayoutManager --- AsyncDisplayKit/TextKit/ASTextKitHelpers.h | 16 +++++++++++++-- AsyncDisplayKit/TextKit/ASTextKitHelpers.mm | 22 +++++++++++++++------ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/AsyncDisplayKit/TextKit/ASTextKitHelpers.h b/AsyncDisplayKit/TextKit/ASTextKitHelpers.h index c66b019f..f9f6fb9e 100644 --- a/AsyncDisplayKit/TextKit/ASTextKitHelpers.h +++ b/AsyncDisplayKit/TextKit/ASTextKitHelpers.h @@ -37,8 +37,20 @@ ASDISPLAYNODE_INLINE CGSize ceilSizeValue(CGSize s) @return An `ASTextKitComponents` containing the created components. The text view component will be nil. @discussion The returned components will be hooked up together, so they are ready for use as a system upon return. */ -+ (ASTextKitComponents *)componentsWithAttributedSeedString:(nullable NSAttributedString *)attributedSeedString - textContainerSize:(CGSize)textContainerSize; ++ (instancetype)componentsWithAttributedSeedString:(nullable NSAttributedString *)attributedSeedString + textContainerSize:(CGSize)textContainerSize; + +/** + @abstract Creates the stack of TextKit components. + @param textStorage The NSTextStorage to use. + @param textContainerSize The size of the text-container. Typically, size specifies the constraining width of the layout, and FLT_MAX for height. Pass CGSizeZero if these components will be hooked up to a UITextView, which will manage the text container's size itself. + @param layoutManager The NSLayoutManager to use. + @return An `ASTextKitComponents` containing the created components. The text view component will be nil. + @discussion The returned components will be hooked up together, so they are ready for use as a system upon return. + */ ++ (instancetype)componentsWithTextStorage:(NSTextStorage *)textStorage + textContainerSize:(CGSize)textContainerSize + layoutManager:(NSLayoutManager *)layoutManager; /** @abstract Returns the bounding size for the text view's text. diff --git a/AsyncDisplayKit/TextKit/ASTextKitHelpers.mm b/AsyncDisplayKit/TextKit/ASTextKitHelpers.mm index 82252b1b..b52761ce 100644 --- a/AsyncDisplayKit/TextKit/ASTextKitHelpers.mm +++ b/AsyncDisplayKit/TextKit/ASTextKitHelpers.mm @@ -19,15 +19,25 @@ @implementation ASTextKitComponents -+ (ASTextKitComponents *)componentsWithAttributedSeedString:(NSAttributedString *)attributedSeedString - textContainerSize:(CGSize)textContainerSize ++ (instancetype)componentsWithAttributedSeedString:(NSAttributedString *)attributedSeedString + textContainerSize:(CGSize)textContainerSize { - ASTextKitComponents *components = [[ASTextKitComponents alloc] init]; + NSTextStorage *textStorage = attributedSeedString ? [[NSTextStorage alloc] initWithAttributedString:attributedSeedString] : [[NSTextStorage alloc] init]; - // Create the TextKit component stack with our default configuration. - components.textStorage = (attributedSeedString ? [[NSTextStorage alloc] initWithAttributedString:attributedSeedString] : [[NSTextStorage alloc] init]); + return [self componentsWithTextStorage:textStorage + textContainerSize:textContainerSize + layoutManager:[[NSLayoutManager alloc] init]]; +} - components.layoutManager = [[NSLayoutManager alloc] init]; ++ (instancetype)componentsWithTextStorage:(NSTextStorage *)textStorage + textContainerSize:(CGSize)textContainerSize + layoutManager:(NSLayoutManager *)layoutManager +{ + ASTextKitComponents *components = [[self alloc] init]; + + components.textStorage = textStorage; + + components.layoutManager = layoutManager; [components.textStorage addLayoutManager:components.layoutManager]; components.textContainer = [[NSTextContainer alloc] initWithSize:textContainerSize];