From 8e18f1562c180e74dbca5d3b8ed3f0d404edc641 Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Thu, 16 Feb 2017 03:54:55 +0800 Subject: [PATCH] [TextKit] Fix text layout issue for CJK laungages (#3026) * [TextKit] Fix text layout issue for CJK laungages * [ASTextKitContext] Delay filling _textStorage with attributedString until calling addLayoutManager. --- AsyncDisplayKit/TextKit/ASTextKitContext.mm | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/AsyncDisplayKit/TextKit/ASTextKitContext.mm b/AsyncDisplayKit/TextKit/ASTextKitContext.mm index 480b3fd46a..79f40c5813 100755 --- a/AsyncDisplayKit/TextKit/ASTextKitContext.mm +++ b/AsyncDisplayKit/TextKit/ASTextKitContext.mm @@ -39,10 +39,18 @@ - (instancetype)initWithAttributedString:(NSAttributedString *)attributedString __instanceLock__ = std::make_shared(); // Create the TextKit component stack with our default configuration. - _textStorage = (attributedString ? [[NSTextStorage alloc] initWithAttributedString:attributedString] : [[NSTextStorage alloc] init]); + + _textStorage = [[NSTextStorage alloc] init]; _layoutManager = [[ASLayoutManager alloc] init]; _layoutManager.usesFontLeading = NO; [_textStorage addLayoutManager:_layoutManager]; + + // Instead of calling [NSTextStorage initWithAttributedString:], setting attributedString just after calling addlayoutManager can fix CJK language layout issues. + // See https://github.com/facebook/AsyncDisplayKit/issues/2894 + if (attributedString) { + [_textStorage setAttributedString:attributedString]; + } + _textContainer = [[NSTextContainer alloc] initWithSize:constrainedSize]; // We want the text laid out up to the very edges of the container. _textContainer.lineFragmentPadding = 0;